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

Help with RegEx expression

jvirtanen0201
6 - Meteoroid

Hello! 

 

I'm struggling with a RegEx issue - I have a template that users are using to provide information, but sometimes the user will delete the last few lines if it doesn't pertain to them. See the template and two examples below; in t he second example, the user has deleted the Amount 2 line because it doesn't pertain to them, whereas in the first example, the user has left the Amount 2 line.

 

Template: 

Name: 

Number:

Amount 1: 

Amount 2: 

 

Examples:

Name: John Smith

Number: 1

Amount 1: 500

Amount 2: 

 

Name: Jack Smith

Number: 2 

Amount 1: 600

 

Using the RegEx Parse expression "Name:(.+)^Number:(.+)^Amount 1:(.+)^Amount 2:(.+)" leaves anything that's like the second example un-parsed since it won't find the Amount 2 line, and I can't quite figure out how to get the expression right. I've also tried RegEx Tokenize with ":(.+)", which only gives me the output from the Name line. 

 

I can use the Text to Columns tool pretty easily, but wanted to try to tackle using RegEx.

 

Thank you in advance!

7 REPLIES 7
NZP
7 - Meteor

I will want to follow this because this is an area that I struggle.

 

Someone recommended to me the following site: https://regex101.com/

 

You can test your regular expression built within this site.

danilang
19 - Altair
19 - Altair

Hi @jvirtanen0201 

 

You can try something like this

 

Name:(.+) Number:(.+) Amount 1: (\d+)\s{0,1}(Amount 2: (.+)){0,1} 

 

I wrapped the entire Amount 2 group in a capturing group and made that one optional {0,1}.  In order to get consistent results I also had to specify that the 

group 1 value was digits {\d+}

 

Results

 

regex.png

 

Dan

ismailhab
7 - Meteor

Hi,

 

Tell me if it helps ? 

Thableaus
17 - Castor
17 - Castor

Hi @jvirtanen0201 

 

This could work:

 

(?:.+?:\s(.*?$))?(?:.+?:\s(.*?$))?(?:.+?:\s(.*?$))?(?:.+?:\s(.*?$))?

 

Basically, each line would represent this:

(?:.+?:\s(.*?$))?

you want everything after the ":\s" but until the end of each line. This whole expression can be optional, which is why we ungroup the whole thing -  (?:......)?

 

A little bit hard to explain, but it works really well and it doesn't give you null values because everything is optional.


Cheers,

jvirtanen0201
6 - Meteoroid

@Thableaus that worked great, thanks so much!!

jvirtanen0201
6 - Meteoroid

@ismailhab thanks so much, that did work!

jvirtanen0201
6 - Meteoroid

@danilang thank you so much, that worked!

Labels