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

RegExp: Add a "-" every 3 characters from right to left

BautistaC888
8 - Asteroid

Hello,
I need a regular expression that transforms expressions like this:
40304023
Into this:
40-304-023
Thank you.

15 REPLIES 15
mpennington
11 - Bolide

I think this should work, given your example.

 

ReverseString(REGEX_Replace(
ReverseString(ToString([Field1])),
"(\d{3})(\d{3})(\d{2})","$1-$2-$3"))

 

 

RegExReverse.jpg 

marcusblackhill
12 - Quasar
12 - Quasar

Hi @BautistaC888 !

 

 

Always have that pattern?( lenght, only numbers)

 

If yes, you don't need regex, can use string formulas for it, like:

left([field1],2)+"-"+substring([field1],3,3)+"-"+right([field1],3)

 

Hope that helps!

mpennington
11 - Bolide

I should note that my solution above is overly complicated, if you just have 8 digits every time, but the format allows you to expand on the RegEx to deal with other scenarios.  If it is always 8 digits, you could essentially work it left to right to accomplish the same thing:

 

Regex_Replace(ToString([Field1]),'(\d{2})(\d{3})(\d{2})','$1-$2-$3')

 

 

 

BautistaC888
8 - Asteroid

No, the patterns are different.

marcusblackhill
12 - Quasar
12 - Quasar

Can share with us some examples of how can appear in you dataset?

mpennington
11 - Bolide

@BautistaC888 , How are they different? What are the limitations?  We would need the parameters, if you want the solution to handle different sized inputs.  

BautistaC888
8 - Asteroid

Here is an example.

mpennington
11 - Bolide

@BautistaC888 , I think you may have forgotten to attach a file or accidentally deleted text from your response--I'm not seeing anything.

BautistaC888
8 - Asteroid

I´m sorry.

Labels