We are celebrating the 10-year anniversary of the Alteryx Community! Learn more and join in on the fun here.
Start Free Trial

Alteryx Designer Desktop Discussions

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

Transform String if substring is present

romain_romain
5 - Atom

Hello, I would like to transform a string if a substring is present inside it.

For example I would like to transform each string containing the substring "Computer" into "Computer Science" and keep the string that don't contain Computer the same as before. The first column is what I have and the second column is what I would like :

Column 1Column 2
Computer NetworkComputer Science
AgricultureAgriculture
Computer SoftwareComputer Science
AccountingAccounting


I tried to do that with a formula by using RegexReplace, my exact formula is :
if REGEX_Match([Column 1], '.*Computer*.') then "Computer Science" else [Column 1] endif

But it is not working.
Anyone has any tips on how to do this?

Thanks in advance.

6 REPLIES 6
randreag
11 - Bolide

Hi @romain_romain 

 

For this you dont need regex a simple if would do it.

 


if Contains([Column 1], '.*Computer*.') then "Computer Science" else [Column 1] endif

 

I hope it helps

Luke_C
17 - Castor
17 - Castor

Hi @romain_romain 

 

Definitely agree with @randreag's solution. For your attempt at regex, you are close. The below should work. I only tweaked the *. to .* after Computer. 

 

if REGEX_Match([Column 1], '.*Computer.*') then "Computer Science" else [Column 1] endif

 

 

A non-regex formula would look very similar:

 

if Contains([Column 1], 'Computer') then "Computer Science" else [Column 1] endif

 

 

apathetichell
20 - Arcturus

Agree with @randreag and @Luke_C  but if you want to do a one line regex - you could do worse than:

REGEX_Replace([Column 1],"(Computer)(.*)","$1 Science")

Luke_C
17 - Castor
17 - Castor

@apathetichell Fancy! Definitely need to brush up more on this

apathetichell
20 - Arcturus

@Luke_C  TBH I wasn't 100% sure I'd get the syntax down so I needed the practice too...

vizAlter
12 - Quasar

Hi @romain_romain — You can also try a "Find Replace" tool:

 

vizAlter_0-1621893103689.png

 

Hope this helps!

Labels
Top Solution Authors