Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.

Regex to Replace Parts of a String

MCoy
7 - Meteor

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

5 REPLIES 5
jdunkerley79
ACE Emeritus
ACE Emeritus

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)

MCoy
7 - Meteor

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.

jdunkerley79
ACE Emeritus
ACE Emeritus

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

MCoy
7 - Meteor

Perfect except one amendment. I had to / each of the brackets (i.e. 

REGEX_REPLACE([Half Year2],"(\d+)_(\d+)","$1\($2\)")

 

Nagi7
5 - Atom

Nagi7_0-1672140512422.png

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

 

Labels