Advent of Code is back! Unwrap daily challenges to sharpen your Alteryx skills and earn badges along the way! Learn more now.

Alteryx Designer Desktop Discussions

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

Regex IF match then replace only second capturing group

athina
7 - Meteor

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
13 - Pulsar

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
21 - Polaris

@athina One way of doing this

 

binuacs_0-1674233938371.png

 

BS_THE_ANALYST
14 - Magnetar
14 - Magnetar

Hi, I can probably help! 

 

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

All the best,
BS

LinkedIN

Bulien
Felipe_Ribeir0
16 - Nebula

I would go with the switch function to do that

 

Felipe_Ribeir0_0-1674234293153.png

 

binuacs
21 - Polaris

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

athina
7 - Meteor

@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