Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Alteryx Designer Desktop Discussions

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

Regex replace function for formatting

fomenter
8 - Asteroid

Hello All,

 

I have multiple words in a string like;

 

I would like to use regex_replace function to convert strings to a specific format like "model 1, tommy srarcha" (with no quotations) so I want to read it backward and stop at the last letter of the first word then put comma, then read it forward until the last word.

 

"tommy srarcha model" = model, tommy srarcha 

"asmali tomas sparak assel"= assel, asmali tomas sparak

etc

 

Thanks for your help

5 REPLIES 5
MarqueeCrew
20 - Arcturus
20 - Arcturus

Quick answer Yes:

Longer answer Yes, but no:

 

Attached is a workflow that parses a field and turns to rows each of the words.  The words are given a word# and all incoming data is sorted so that the last word is first and the first word is last. The data is then put back together with (SPACE) as the delimiter.  The first space is replaced with a comma followed by a space.  You get what you wanted, but I didn't use RegEx to do it.

 

Cheers,

Mark

 

 

Alteryx ACE & Top Community Contributor

Chaos reigns within. Repent, reflect and restart. Order shall return.
Please Subscribe to my youTube channel.
Bob_Blackey
11 - Bolide

I love  any chance to use Regex, it's like playing an instrument, if you don't practice every week you get rusty. So I'll add my two cents.

 

It uses Regex Parse to separate the last word from the rest and then a formula to put it all together

 

regex_reverse.png

 

 

 

 

Cheers,

Bob

 

MarqueeCrew
20 - Arcturus
20 - Arcturus

@Bob_Blackey,

 

I agree with you and want you to know that I over-engineered.

 

I thought that we had to reverse all of the words.  That's why I didn't go down a regex path.

 

You took the last word added a comma and space, then appended the rest of the original data.

 

One Two Three would become:  Three, One Two

 

I made the answer:  Three, Two One.

 

My bad

Alteryx ACE & Top Community Contributor

Chaos reigns within. Repent, reflect and restart. Order shall return.
Please Subscribe to my youTube channel.
Bob_Blackey
11 - Bolide

@MarqueeCrew

 

Been there, done that! It's five o'clock somewhere :-)

 

 

jdunkerley79
ACE Emeritus
ACE Emeritus

It is possible as a regex_replace:

REGEX_Replace([input], "(.*) (\w+)", "$2, $1")

This will put last word first

 

If you need to cope with trailing number like `tommy srarcha model 1` then a little more complicated:

REGEX_Replace([input], "(.*) ([A-Za-z]+ ?\d*)", "$2, $1")

 

Labels