Skip to main content
Solved

Can I apply string functions to regex replacement text in a StringReplacer transformer?


tfcw
Participant
Forum|alt.badge.img+1
  • Participant

In my workbench I have a column full of strings where I'd like certain sequences of characters to be capitalized. I could do this by adding a StringReplacer for each sequence, but I have a lot of sequences and I'd rather not do that if I can solve the problem with a general solution. What I'd like to do is just use some StringReplacer like

Attributes: RAILWAY_NAME

Text to Find: Cpr|Cnr

Replacement Text: @UpperCase(\\0)

Use Regular Expressions: Yes

Case Sensitive: Yes

Where whatever it finds would be replaced with the upper case version. But when I try this on the string "Cnr Railway" nothing gets replaced. If I try it with the replacement text \\0+1 I get "Cnr+1 Railway", so I know the regex is matching correctly. If I try it with the replacement text @StringLength(\\0) I get "2 Railway", so it looks like functions take "\\0" as a literal string. But if that were the case why does @UpperCase(\\0) return "Cnr Railway"?

So my quesiton is:

1. Can I use FME String Functions on regex replacement text in a StringReplacer.

2. If yes, how. If no, is there an easy way to capitalize certain sequences of characters in a string.

I'm using FME Desktop 2013, if it matters.

Best answer by takashi

Hi @yola, can the StringPairReplacer help you?

  • Replacement Pairs: Cpr CPR Cnr CNR
View original
Did this help you find an answer to your question?

8 replies

ebygomm
Influencer
Forum|alt.badge.img+31
  • Influencer
  • March 31, 2016

Not sure whether this would work in 2013 or not, but this works in 2015

StringSearcher - Regular Expression as above

AttributeCreator - create a new attribute with the matched characters turned to uppercase

Stringreplacer - use the matched characters as text to match with the new attribute created above as replacement text


erik_jan
Contributor
Forum|alt.badge.img+17
  • Contributor
  • March 31, 2016

Would it not be easier to use two StringReplacers in sequence in this case?

And no regular expressions, just replace Cnr with CNR and Cpr with CPR.


ebygomm
Influencer
Forum|alt.badge.img+31
  • Influencer
  • March 31, 2016
erik_jan wrote:

Would it not be easier to use two StringReplacers in sequence in this case?

And no regular expressions, just replace Cnr with CNR and Cpr with CPR.

I presume in reality the regular expression will be matching far more than 2 distinct letter sequences.


tfcw
Participant
Forum|alt.badge.img+1
  • Author
  • Participant
  • March 31, 2016
ebygomm wrote:

I presume in reality the regular expression will be matching far more than 2 distinct letter sequences.

Yup, I've got more than 2 sequences, and possibly more in the future. So I'd rather not add another transformer anytime a new one pops up


tfcw
Participant
Forum|alt.badge.img+1
  • Author
  • Participant
  • March 31, 2016
ebygomm wrote:

Not sure whether this would work in 2013 or not, but this works in 2015

StringSearcher - Regular Expression as above

AttributeCreator - create a new attribute with the matched characters turned to uppercase

Stringreplacer - use the matched characters as text to match with the new attribute created above as replacement text

This helps a lot but it's not quite what I need. Each string I take in possibly has more than one sequence within it, and this only replaces the first sequence it finds in a string.

But this does help a lot and I think with some tinkering it'll probably work, thanks.


takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • Best Answer
  • March 31, 2016

Hi @yola, can the StringPairReplacer help you?

  • Replacement Pairs: Cpr CPR Cnr CNR

tfcw
Participant
Forum|alt.badge.img+1
  • Author
  • Participant
  • April 1, 2016
takashi wrote:

Hi @yola, can the StringPairReplacer help you?

  • Replacement Pairs: Cpr CPR Cnr CNR

Yep, perfect. Thanks!


takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • April 2, 2016
tfcw wrote:

Yep, perfect. Thanks!

Good to hear :-)

It seems that a string function within the "Replacement Text" field is evaluated before the regex replacement. That is, "@UpperCase(\\0)" will be translated to "\\0" before replacing. Result, the input string will not be changed anyway. I think this is the reason why you cannot get your expected result from the function use.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings