Options

[SOLVED] regex and string-parsing question

tennenrishintennenrishin Member Posts: 177 Contributor II
edited November 2018 in Help
I hope my question comes across okay.

Suppose...
We have macros %{A} and %{P}. Where...
%{P} contains some token (say #1) which serves as a kind of place-holder/wildcard.
%{A} is some unknown string, but it matches %{P} in the sense that if we replace #1 in %{P} with some known regex (say .*) then the result matches %{A}

Question:
Is there a way to obtain that part (substring) of %{A} that matches the .* that replaced #1 in %{P}?
In other words is there a way to determine what #1 represented in %{A}?

Simplistic examples:

%{A}=abcd
%{P}=a#1d
Required output: bc

%{A}=abcde
%{P}=ab#1
Required output: cde

Further comments:
I've read up a bit on "capturing groups" in regular expressions but I can't figure out a way to access what is captured by a group and put it into another macro.

Answers

  • Options
    MariusHelfMariusHelf RapidMiner Certified Expert, Member Posts: 1,869 Unicorn
    Hi,

    I have no idea why you want to do it with macros, but everything is possible with RapidMiner :) The trick is to write A to an example set first and then use the Replace operator. Please have a look at the attached process.

    Best, Marius
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <process version="5.2.009">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="5.2.009" expanded="true" name="Process">
        <process expanded="true" height="420" width="706">
          <operator activated="true" class="set_macro" compatibility="5.2.009" expanded="true" height="76" name="Set Macro A" width="90" x="45" y="30">
            <parameter key="macro" value="A"/>
            <parameter key="value" value="abcd"/>
          </operator>
          <operator activated="true" class="set_macro" compatibility="5.2.009" expanded="true" height="76" name="Set Macro B" width="90" x="179" y="30">
            <parameter key="macro" value="B"/>
            <parameter key="value" value="a#1d"/>
          </operator>
          <operator activated="true" class="generate_macro" compatibility="5.2.009" expanded="true" height="76" name="Parse B" width="90" x="313" y="30">
            <list key="function_descriptions">
              <parameter key="B_parsed" value="replace(&quot;%{B}&quot;, &quot;#1&quot;, &quot;(.*)&quot;)"/>
            </list>
          </operator>
          <operator activated="true" class="generate_data_user_specification" compatibility="5.2.009" expanded="true" height="60" name="Generate Data by User Specification" width="90" x="45" y="165">
            <list key="attribute_values">
              <parameter key="A" value="&quot;%{A}&quot;"/>
            </list>
            <list key="set_additional_roles"/>
          </operator>
          <operator activated="true" class="replace" compatibility="5.2.009" expanded="true" height="76" name="Replace" width="90" x="179" y="165">
            <parameter key="replace_what" value="%{B_parsed}"/>
            <parameter key="replace_by" value="$1"/>
          </operator>
          <connect from_op="Set Macro A" from_port="through 1" to_op="Set Macro B" to_port="through 1"/>
          <connect from_op="Set Macro B" from_port="through 1" to_op="Parse B" to_port="through 1"/>
          <connect from_op="Generate Data by User Specification" from_port="output" to_op="Replace" to_port="example set input"/>
          <connect from_op="Replace" from_port="example set output" to_port="result 1"/>
          <portSpacing port="source_input 1" spacing="0"/>
          <portSpacing port="sink_result 1" spacing="126"/>
          <portSpacing port="sink_result 2" spacing="0"/>
        </process>
      </operator>
    </process>
  • Options
    tennenrishintennenrishin Member Posts: 177 Contributor II
    Wow, thanks! That is exactly what I was looking for.
Sign In or Register to comment.