Skip to main content
Question

String Replacer with Regex in Condition Statement

  • January 17, 2018
  • 9 replies
  • 133 views

robertr
Contributor
Forum|alt.badge.img+6

Hello,

 

I was trying to use this transformer to replace some matched patterns but I cannot get my head around how this should be working.

 

I am confused about having the same fields to fill in both under TextToRepace Condition Statement and the TextToReplace.

 

 

What I want to accomplish is to obtain the City name from the below text strings.

 

As you can see, I have 2 cases that need to be solved:

 

 

#1 - replace "^S2_XLS_\\D\\d{3}_" with nothing

 

#2 - pull out "(?!^\\w\\d{3})_\\D+?_" from the rest of the cases.

 

I find it a bit hard to understand how this should look, chained in/or the Conditional Statements of both "Text to Replace" and "Replacement Text".

 

 

Can anyone point me into the right direction?

 

 

I very much appreciate this.

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

9 replies

jneujens
Forum|alt.badge.img
  • 189 replies
  • January 17, 2018

That is how I would do it. See screenshot below for the details.

Use a StringSearcher with 1 of your 2 regex statements. This will separate both types in a Matched and NonMatched port. Connect both ports to a separate StringReplacer and use mode 'Replace Regular Expression'. Make sure you don't use the " sign around the statement and leave the 'Replace Text field' blank. Afterwards you can put both flows of feature back together without problems.


oscard
Influencer
Forum|alt.badge.img+22
  • Influencer
  • 344 replies
  • January 17, 2018
I would use a Tester to filter the data. Then, two StringReplacer transformers. One for case 1 and the other for case 2.

 

 

Something like this in the Tester:

 

 

If Value(city) starts with "S2_XLS" (passed) --> StringReplacer_1

 

Else (failed) --> StringReplacer_2

 


takashi
Celebrity
  • 7843 replies
  • January 17, 2018

Hi @robertr, if there were only these two patterns:

  • Starting with 'S'; four parts delimited by underscore; the last part is a City name
  • Starting with 'W'; three parts delimited by underscore; the second part is a City name

the StringReplacer with these parameters might be a solution.

  • Mode: Replace Regular Expression
  • Text to Replace: ^(S.+_.+_.+|W.+?)_(.+?)(_.+)?$
  • Replacement Text: \\2

 


robertr
Contributor
Forum|alt.badge.img+6
  • Author
  • Contributor
  • 60 replies
  • January 17, 2018

That is how I would do it. See screenshot below for the details.

Use a StringSearcher with 1 of your 2 regex statements. This will separate both types in a Matched and NonMatched port. Connect both ports to a separate StringReplacer and use mode 'Replace Regular Expression'. Make sure you don't use the " sign around the statement and leave the 'Replace Text field' blank. Afterwards you can put both flows of feature back together without problems.

Hello @jneujens,

 

Thank you for the reply. It would work but I am trying not to reuse transformers when it could be done in one go.

 

 

Above you have what I was looking for, see @takashi's reply.

 

I will also post what I came up with, slightly different.

robertr
Contributor
Forum|alt.badge.img+6
  • Author
  • Contributor
  • 60 replies
  • January 17, 2018
I would use a Tester to filter the data. Then, two StringReplacer transformers. One for case 1 and the other for case 2.

 

 

Something like this in the Tester:

 

 

If Value(city) starts with "S2_XLS" (passed) --> StringReplacer_1

 

Else (failed) --> StringReplacer_2

 

Hello @oscard,

 

Thank you for taking time to give your input. It is a valid solution but I was looking not to reuse transformers. Generally, if one has too many of the same transformer, it is likely that it can be solved in one go.

 

 

As an example there is @takashi's version and I will post one more soon.

robertr
Contributor
Forum|alt.badge.img+6
  • Author
  • Contributor
  • 60 replies
  • January 17, 2018

Hi @robertr, if there were only these two patterns:

  • Starting with 'S'; four parts delimited by underscore; the last part is a City name
  • Starting with 'W'; three parts delimited by underscore; the second part is a City name

the StringReplacer with these parameters might be a solution.

  • Mode: Replace Regular Expression
  • Text to Replace: ^(S.+_.+_.+|W.+?)_(.+?)(_.+)?$
  • Replacement Text: \\2

 

Hello @takashi,

 

Yes, this is what I was looking for. I managed to solve it in the same transformer too, using a slightly different approach as to learn how the conditional value works.

 

 

As an addendum, I find it very unintuitive as you only get a "<string>" hint when you open a Function via Text Editor:

 

- it does not mention that you should not ad double quotes around the Regex, nor if you can use a Regex as a replace string. One would be inclined to use a string. Documentation is also very light and I did not figure out from the start if we can use the group reference.

 

 

Anyways, below is my version too, a bit more complicated than yours but it does the job:

 

 

 

Thanks for the prompt reply!

 

 


oscard
Influencer
Forum|alt.badge.img+22
  • Influencer
  • 344 replies
  • January 17, 2018
Hello @oscard,

 

Thank you for taking time to give your input. It is a valid solution but I was looking not to reuse transformers. Generally, if one has too many of the same transformer, it is likely that it can be solved in one go.

 

 

As an example there is @takashi's version and I will post one more soon.
Good call :)

 

 


takashi
Celebrity
  • 7843 replies
  • January 20, 2018

Hi @robertr, if there were only these two patterns:

  • Starting with 'S'; four parts delimited by underscore; the last part is a City name
  • Starting with 'W'; three parts delimited by underscore; the second part is a City name

the StringReplacer with these parameters might be a solution.

  • Mode: Replace Regular Expression
  • Text to Replace: ^(S.+_.+_.+|W.+?)_(.+?)(_.+)?$
  • Replacement Text: \2

 

If you would use conditional value setting and FME String functions such as @ReplaceRegEx, the solution might be simpler. e.g.

 

0684Q00000ArMGiQAN.png

If @Value(_dirname) Begins With S Then
    @ReplaceRegEx(@Value(_dirname),".+_(?=.+?$)","")
Else If @Value(_dirname) Begins With W Then
    @ReplaceRegEx(@Value(_dirname),".+_(.+)_.+",\1) 
You can see detailed descriptions on FME String Functions here.

 

FME Workbench Help | String Functions

 

 


robertr
Contributor
Forum|alt.badge.img+6
  • Author
  • Contributor
  • 60 replies
  • January 23, 2018
If you would use conditional value setting and FME String functions such as @ReplaceRegEx, the solution might be simpler. e.g.

 

0684Q00000ArMGiQAN.png

If @Value(_dirname) Begins With S Then
    @ReplaceRegEx(@Value(_dirname),".+_(?=.+?$)","")
Else If @Value(_dirname) Begins With W Then
    @ReplaceRegEx(@Value(_dirname),".+_(.+)_.+",\1) 
You can see detailed descriptions on FME String Functions here.

 

FME Workbench Help | String Functions

 

 

Yes, that is true. Easier but it was helpful to see how these different ways work in FME.

 

I'm glad I got the conditions working and that I now know how regex fits in.