Skip to main content
Solved

Use \\1 in Replacement Text in StringReplacer

  • November 9, 2018
  • 3 replies
  • 292 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

 

Best answer by david_r

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
View original
Did this help you find an answer to your question?

3 replies

ebygomm
Influencer
Forum|alt.badge.img+38
  • Influencer
  • November 9, 2018

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


david_r
Celebrity
  • Best Answer
  • November 9, 2018

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

  • Author
  • November 9, 2018
david_r wrote:

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!


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