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!
Solved! Go to Solution.
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.
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
Dan
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,
@Thableaus that worked great, thanks so much!!
@ismailhab thanks so much, that did work!
@danilang thank you so much, that worked!