Skip to main content
Solved

RegEx to replace first instance in string

  • February 12, 2025
  • 5 replies
  • 152 views

liz.g
Contributor
Forum|alt.badge.img+1
  • Contributor

Hello,

 

I am seeking help with using a Regex to replace the first instance of an underscore in a string.

 

I am using a BulkAttributeRenamer to rename a series of attributes with multiple underscores like this as an example: 0_4_C11_M

 

I am trying to replace just the first underscore in the string with a dash with a regex replace to change the name from 0_4_C11_M to 0-4_C11_M. The regex I’m using is ([\_]+)

 

When I test that expression in RegEx101 is does exactly what I need and only replaces the first underscore in the string as shown below:

However, when I use the same expression in the BulkAttributeRenamer it’s returning and replacing all underscores as shown below:

Hoping someone can help with advice on how to limit the return to just the first underscore in the string please - it the expression incorrect or is there something else I need to add to the parameters? Thanks :)

Best answer by bwn

With replacements, I usually use a more explicit form in RegEx to avoid this.  The default technique I use is to replace the entire string

 

Instead of grouping the “_” ….. instead group the characters before and after the “_” like this:

(^[^_]*)\_(.*$)

Then replacement string to use in FME is:

\1-\2

ie. Replace the entire string with Group 1 characters (before the “_”) + “-” +  Group 2 characters (after the “_”)

View original
Did this help you find an answer to your question?
This post is closed to further activity.
It may be a question with a best answer, an implemented idea, or just a post needing no comment.
If you have a follow-up or related question, 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.

5 replies

bwn
Evangelist
Forum|alt.badge.img+26
  • Evangelist
  • Best Answer
  • February 13, 2025

With replacements, I usually use a more explicit form in RegEx to avoid this.  The default technique I use is to replace the entire string

 

Instead of grouping the “_” ….. instead group the characters before and after the “_” like this:

(^[^_]*)\_(.*$)

Then replacement string to use in FME is:

\1-\2

ie. Replace the entire string with Group 1 characters (before the “_”) + “-” +  Group 2 characters (after the “_”)


liz.g
Contributor
Forum|alt.badge.img+1
  • Author
  • Contributor
  • February 13, 2025

Thank you so much ​@bwn  - that works perfectly!

Really appreciate your help! 


danilo_fme
Evangelist
Forum|alt.badge.img+45
  • Evangelist
  • February 13, 2025
bwn wrote:

With replacements, I usually use a more explicit form in RegEx to avoid this.  The default technique I use is to replace the entire string

 

Instead of grouping the “_” ….. instead group the characters before and after the “_” like this:

(^[^_]*)\_(.*$)

Then replacement string to use in FME is:

\1-\2

ie. Replace the entire string with Group 1 characters (before the “_”) + “-” +  Group 2 characters (after the “_”)

Great solution!


ebygomm
Influencer
Forum|alt.badge.img+36
  • Influencer
  • February 13, 2025

Just as an FYI, if you are using regex 101 for testing you want to make sure global is enabled, as this is generally how FME is implementing regular expressions

 


liz.g
Contributor
Forum|alt.badge.img+1
  • Author
  • Contributor
  • February 13, 2025

Thanks so much for the tip ​@ebygomm - much appreciated! :)


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