Alteryx Designer Desktop Discussions

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

Dynamic rename with regex

cireost
8 - Asteroid

can someone please help with the expression to replace the spaces after the numbers with a dash using RegEx?  this is going into a dynamic rename tool.  thanks!

 

Example:

 

Before:

1234 4545 Building

334343 34345 Car

938 34343 Equipment

 

After:

1234-4545 - Building

334343-34345 - Car

938-34343 - Equipment

5 REPLIES 5
Thableaus
17 - Castor
17 - Castor

Hi @cireost 

 

REGEX_Replace([Field], "\s", "-")

 

This should work.

 

Cheers,

cireost
8 - Asteroid

@Thableaus  sorry missing one detail.

 

some of the text are two words so I only want the dashes in between the numbers and after the numbers. but not in between the words.  thanks!

 

Before:

3934 3493 New Building

 

After

3934-3493 - New Building

Thableaus
17 - Castor
17 - Castor

@cireost 

 

If the pattern always consists of two sequences of digits (with whitespace in between), whitespace and anything else.

 

You can set this: REGEX_Replace([Field1], "(\d+)\s+(\d+)\s+(.*)", "$1-$2 - $3")

 

Cheers,

cireost
8 - Asteroid

@Thableaus awesome, this works!

 

would you mind just explaining to me how the formula works?  i have very basic understanding of regex.  I know what d+ and s+ are but not sure how the groupings ( ) and $ works.

 

 

Thableaus
17 - Castor
17 - Castor

@cireost 

 

First, this website https://www.rexegg.com/ has a lot of good content on explaining how RegEx works.

 

But I will try to briefly explain groupings and $.

 

Captured groups are used to reference patterns. If you capture a part of your RegEX, you can use it later - best example is the Replace method.

$1, $2, $3 - refer exactly to these captured groups

They are like stored variables - in my example, I captured the digits ($1 and $2) and everything after the whitespace (.*)

Then in the Replace Method, I referenced them and replaced the \s+ for everything I needed (in this case, a dash).

 

Cheers,

Labels