Extracting a number from a string under specific conditions

paolochip
paolochip
Altair Employee
edited November 2024 in Community Q&A
Hi,
I'm struggling on doing something that looks easy (and maybe dumb), but I don't find a way. I need to extract a number from a string that matches specific conditions. I have a column with different strings like:

John Alpha U23
Mike Beta C57
Ann Cole U73456
Amy Utta J94857

I want RM to create a new colum that picks only numbers where there is the U in front and leave blank (or put something like none) on the other rows

23
none
73456
none

I'm not able to uniquely identify strings that contains both U and a number after. Is there a way to do that without using python or generativeAI blocks? By using a Split block with a regular expression like [^U[0-9]]{1,} it picks also the U of Amy, while I want select U only if it has followed by a number.

Thanks,
Paolo

Welcome!

It looks like you're new here. Sign in or register to get started.

Answers

  • MartinLiebig
    MartinLiebig
    Altair Employee
    edited October 2023
    Here we go, this should do it:

    Spoiler
    <?xml version="1.0" encoding="UTF-8"?><process version="10.2.000">
    
    <context>
    <input/>
    <output/>
    <macros/>
    </context>
    <operator activated="true" class="process" compatibility="10.2.000" expanded="true" name="Process">
    <parameter key="logverbosity" value="init"/>
    <parameter key="random_seed" value="2001"/>
    <parameter key="send_mail" value="never"/>
    <parameter key="notification_email" value=""/>
    <parameter key="process_duration_for_mail" value="30"/>
    <parameter key="encoding" value="SYSTEM"/>
    <process expanded="true">
    <operator activated="true" class="utility:create_exampleset" compatibility="10.2.000" expanded="true" height="68" name="Create ExampleSet" width="90" x="246" y="34">
    <parameter key="generator_type" value="comma separated text"/>
    <parameter key="number_of_examples" value="100"/>
    <parameter key="use_stepsize" value="false"/>
    <list key="function_descriptions"/>
    <parameter key="add_id_attribute" value="false"/>
    <list key="numeric_series_configuration"/>
    <list key="date_series_configuration"/>
    <list key="date_series_configuration (interval)"/>
    <parameter key="date_format" value="yyyy-MM-dd HH:mm:ss"/>
    <parameter key="time_zone" value="SYSTEM"/>
    <parameter key="input_csv_text" value="attribute&#10;John Alpha U23&#10;Mike Beta C57&#10;Ann Cole U73456&#10;Amy Utta J94857"/>
    <parameter key="column_separator" value=","/>
    <parameter key="parse_all_as_nominal" value="true"/>
    <parameter key="decimal_point_character" value="."/>
    <parameter key="trim_attribute_names" value="true"/>
    </operator>
    <operator activated="true" class="generate_copy" compatibility="10.2.000" expanded="true" height="82" name="Generate Copy" width="90" x="447" y="34">
    <parameter key="attribute_name" value="attribute"/>
    <parameter key="new_name" value="number"/>
    </operator>
    <operator activated="true" class="replace" compatibility="10.2.000" expanded="true" height="82" name="Replace" width="90" x="581" y="34">
    <parameter key="attribute_filter_type" value="single"/>
    <parameter key="attribute" value="number"/>
    <parameter key="attributes" value=""/>
    <parameter key="use_except_expression" value="false"/>
    <parameter key="value_type" value="nominal"/>
    <parameter key="use_value_type_exception" value="false"/>
    <parameter key="except_value_type" value="file_path"/>
    <parameter key="block_type" value="single_value"/>
    <parameter key="use_block_type_exception" value="false"/>
    <parameter key="except_block_type" value="single_value"/>
    <parameter key="invert_selection" value="false"/>
    <parameter key="include_special_attributes" value="false"/>
    <parameter key="replace_what" value="[\w|\s]+ U(\d+)"/>
    <parameter key="replace_by" value="$1"/>
    </operator>
    <operator activated="true" class="parse_numbers" compatibility="10.2.000" expanded="true" height="82" name="Parse Numbers" width="90" x="715" y="34">
    <parameter key="attribute_filter_type" value="single"/>
    <parameter key="attribute" value="number"/>
    <parameter key="attributes" value=""/>
    <parameter key="use_except_expression" value="false"/>
    <parameter key="value_type" value="nominal"/>
    <parameter key="use_value_type_exception" value="false"/>
    <parameter key="except_value_type" value="file_path"/>
    <parameter key="block_type" value="single_value"/>
    <parameter key="use_block_type_exception" value="false"/>
    <parameter key="except_block_type" value="single_value"/>
    <parameter key="invert_selection" value="false"/>
    <parameter key="include_special_attributes" value="false"/>
    <parameter key="decimal_character" value="."/>
    <parameter key="grouped_digits" value="false"/>
    <parameter key="grouping_character" value=","/>
    <parameter key="infinity_representation" value=""/>
    <parameter key="unparsable_value_handling" value="replace with missing values"/>
    </operator>
    <connect from_op="Create ExampleSet" from_port="output" to_op="Generate Copy" to_port="example set input"/>
    <connect from_op="Generate Copy" from_port="example set output" to_op="Replace" to_port="example set input"/>
    <connect from_op="Replace" from_port="example set output" to_op="Parse Numbers" to_port="example set input"/>
    <connect from_op="Parse Numbers" from_port="example set output" to_port="result 1"/>
    <portSpacing port="source_input 1" spacing="0"/>
    <portSpacing port="sink_result 1" spacing="0"/>
    <portSpacing port="sink_result 2" spacing="0"/>
    </process>
    </operator>
    </process>





  • paolochip
    paolochip
    Altair Employee
    It works! :)
    Thanks a lot for your super fast reply!
  • CKönig
    CKönig New Altair Community Member
  • Tiimm
    Tiimm New Altair Community Member
    which operator is used for this?

    Icoon voor Geverifieerd door de community

Welcome!

It looks like you're new here. Sign in or register to get started.

Welcome!

It looks like you're new here. Sign in or register to get started.