Regex to Replace Parts of a String
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi,
I searched and found posts but still struggling to get my head round an example. What I am trying to do is take the following entries in my data....
2014_1
2014_2
2015_1
2015_2
2015_1
and make them read
2014(1)
2014(2)
2015(1)
2015(2)
2015(1)
In essence replacing the "_" with"(" and placing a ")" at the end of the line.
Grateful for help...
Thanks
- Labels:
- Regex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Using a formula tool, something like:
REGEX_REPLACE([Field1],"(\d+)_(\d+)","$1($2)")
should do what you need.
Picks first block of numbers up to the _ and this becomes $1
Picks second block of numbers after the _ and this becomes $2
Then replaces it with $1($2)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Many thanks.
I see you're suggesting the Formula Tool - is this preferable to using the RegEx tool? (Just for my broader understanding).
Thanks again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Doesnt make much difference in this case. Result would be identical I think.
For parsing has to Regex tool otherwise both equivalent.
If using the REGEX tool then the (\d+)_(\d+) is the the Regular Expression and the $1($2) goes in the Repalcement Text
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Perfect except one amendment. I had to / each of the brackets (i.e.
REGEX_REPLACE([Half Year2],"(\d+)_(\d+)","$1\($2\)")
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hello,
I would like to remove what's between parentheses as well as region names from the above. Essentially, I would like to only keep the commodity (Gas, coal...) Can this be done using a single regexp_replace formula?
Thanks