RegEx tool with Parse method and OR (|)
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi All,
Ok I admit that I'm no RegEx expert so please forgive my ignorance.
I'm trying to use the tool with the Parse method and the OR (|) operator to find two items in a string where the values could be in different locations but I can't get the thing to work and wondered if anyone knew what I'm missing but also how the tool works in this scenario. The fields I'm trying to get are a name in single quotes and a duration. I know this can be solved by other methods /multiple regex tools but it's the 'why' that is just doing my head in.
Sample text (items in bold=text I'm trying to get):
- Complete 'the wobbly bridge' (Succeeded, Id=123456 Duration=247ms)
Complete (Succeeded, Id=123456 Duration=247ms) 'the wobbly bridge'
- If I use the tool with simple regex of just '(.+)' then I get the wobbly bridge
- If I use the tool with regex of just n=(\d+) then I get 247
- If I combine these with '(.+)'|n=(\d+) then it I get the following
The expression works fine in https://regex101.com/r/waP9rd/1
From the looks of things it's just finding the first item.
Thank you.
- Labels:
- Regex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi @brandowd
I'm glad you're getting excited about RegEx and trying to figure out how it works in Alteryx!
Typically, I actually do my RegEx expressions in a Formula tool, but it seems as though your expression isn't working because Alteryx is trying to put these groups into actual fields based on your pattern. So the first field created will always be the '(.+)' group. What if, instead, you tried something like this where you're capturing the two different possible orders with the "or" between them? This will create 4 fields but you can easily combine those into your 2 desired fields with a regular formula tool after.
'(.*)'.*n=(\d+)|n=(\d+).*'(.*)'
On the other hand, if you wanted to try the formula tool method, you could explicitly create two new fields, calling them what you wish and using the REGEX_Replace expression. To get the name group, it would look something like this
REGEX_Replace([Field1], ".*'(.*)'.*", "$1")
If you like this idea, try to create the duration field in a similar way!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hey @brandowd, are you precious about using the ‘or’/doing this in one? If not, you could just use 2 RegEx tools and parse out one thing at a time.
With the way you’re current set up, the tool is looking through the field with the condition of ‘go until I reach X or Y’. So in your first function, it is going until it reaches the first of your defined capture groups, which is the wobbly bridge, whereas in the second it is the other way round so the first instance of a capture group it finds is 247, then stops.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
haha @Kenda, i had your solution ready as well ^^:
'(.+)'.*n=(\d+)|n=(\d+).*'(.+)'
To bad Alteryx' Regex tool doesn't alway work exactly as Regex101, this costed me like 20 min to figure out haha. Still love Alteryx tough :-)!
Greetings,
Seb
