Solved

Use \\1 in Replacement Text in StringReplacer

  • 9 November 2018
  • 3 replies
  • 44 views

Hi

I am trying to convert attribute values from PascalCase or camelCase to underscore_separated in FME 2018.1.

 

I've tried StringReplacer in "Replace Regular Expression" mode. Text To Replace is [A-Z] and Replacement Text is _\\1. It is matching the text to replace correctly but rather than substituting the capture group it is replacing with literally _\\1 so schoolCode becomes school_\\1ode instead of school_Code.

I've also tried to use @ReplaceRegEx(@Value(name),[A-Z],_\\1 ) on an attributecreator with exactly the same result.

Everything I can find (including StringReplacer help) suggests that \\1 is the way to do what I want, but I just can't get it to work!

Any help would be really appreciated.

Thanks

Tom

 

icon

Best answer by david_r 9 November 2018, 15:27

View original

3 replies

Userlevel 1
Badge +21

Try brackets round the text to replace expression, so ([A-Z])

Userlevel 4

I agree with @egomm, you need to put paranthesis around the replace expression. This is because "\1" is a back reference to a capture group, which corresponds to the contents of what was matched inside a paranthesis.

That said, you will discover that the posted regex will convert "HeyYou" to "_hey_you" which may or may not be what you need.

You can consider using the following setup to let "HeyYou" be replaced as "hey_you", if needed.

Text to replace:

([a-z])([A-Z]+)

Replacement text:

\1_\2

I agree with @egomm, you need to put paranthesis around the replace expression. This is because "\1" is a back reference to a capture group, which corresponds to the contents of what was matched inside a paranthesis.

That said, you will discover that the posted regex will convert "HeyYou" to "_hey_you" which may or may not be what you need.

You can consider using the following setup to let "HeyYou" be replaced as "hey_you", if needed.

Text to replace:

([a-z])([A-Z]+)

Replacement text:

\1_\2

Thanks very much, great answer. I had tried all manner of combinations of brackets around the replacement text but not on the text to replace. It makes perfect sense now that there was no capture group to insert. I'm one step closer to understanding RegEx, just a few more thousand to go!

Reply