Early bird tickets for Inspire 2023 are now available! Discounted pricing closes on January 31st. Save your spot!

Alteryx Designer Discussions

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

Regex IF match then replace only second capturing group

athina
6 - Meteoroid

Hi community! I have a question - can I use the formula tool to do :

  • a regex match to match my two capturing groups
  • then, if my second capturing group is x, replace it with y

I can do it via multiple steps, but I'm eager to do it in fewer steps if possible.

 

So my data looks like this

 

202104
202105
202106

 

What I want is to change this column to read

 

2021-Monday
2021-Tuesday
2021-Wednesday

 

So I want to keep the first four digits i.e. the first capturing group, and then replace the second capturing group by saying IF 04 then Monday, IF 05 then Tuesday, IF 06 then Wednesday etc. I've used this expression 

 

IF REGEX_Match([Period], "(\d){4}(04)") then "Monday" elseif
IF REGEX_Match([Period], "(\d){4}(05)") then "Tuesday" etc

 

And then via multiple steps I get to my desired format.

 

Is there any way to get to my output in fewer steps?!

 

Hope this makes sense and thanks for any tips!

6 REPLIES 6
FinnCharlton
11 - Bolide

Hi @athina , I don't think you need any regex for this problem, you can do it with one formula:

FinnCharlton_0-1674233947713.png

 

binuacs
17 - Castor

@athina One way of doing this

 

binuacs_0-1674233938371.png

 

BS_THE_ANALYST
9 - Comet

Hi, I can probably help! 

 

I just want to understand, why does 04 map to Monday, 05 to Tuesday. 06 to Wednesday? 

Felipe_Ribeir0
15 - Aurora

I would go with the switch function to do that

 

Felipe_Ribeir0_0-1674234293153.png

 

binuacs
17 - Castor

@BS_THE_ANALYST my understanding was 202104 : 2021-01-04 which is Monday. 

athina
6 - Meteoroid

@binuacs@Felipe_Ribeir0 , and @FinnCharlton thank you both! All three perfect solutions that worked, I've accepted them all, just to say that I think I'll be going with @Felipe_Ribeir0 as I had never used 'switch' before and it was cool to use it :)

 

And of course you are right that I didn't need regex to do this (although I enjoyed the riddle of trying to do it with one single regex formula) 

 

Cheers all!

Labels