Skip to main content
Solved

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

  • March 31, 2016
  • 8 replies
  • 63 views

tfcw
Contributor
Forum|alt.badge.img+1

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
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.

8 replies

ebygomm
Influencer
Forum|alt.badge.img+46
  • 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+23
  • 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+46
  • Influencer
  • 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.

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


tfcw
Contributor
Forum|alt.badge.img+1
  • Author
  • Contributor
  • March 31, 2016

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
Contributor
Forum|alt.badge.img+1
  • Author
  • Contributor
  • 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

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
Celebrity
  • Best Answer
  • March 31, 2016

Hi @yola, can the StringPairReplacer help you?

  • Replacement Pairs: Cpr CPR Cnr CNR

tfcw
Contributor
Forum|alt.badge.img+1
  • Author
  • Contributor
  • April 1, 2016

Hi @yola, can the StringPairReplacer help you?

  • Replacement Pairs: Cpr CPR Cnr CNR

Yep, perfect. Thanks!


takashi
Celebrity
  • April 2, 2016

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.