Hi
I am trying to grab a string within brackets using Reg Ex. The output should be as below.
Field | Output |
adfsdfsfaw reg asdfsdfas dsdfsdf (regulation x) | regulation x |
asdf33 sdfsd cc (regulation yy) | regulation yy |
asdfsdf erer reg sdfsd | |
sdfs werwewrwer (regulation ttt) | regulation ttt |
I found the below solution and it works - can someone pls explain why though? How do I read this?
(.*)(?:\()(.*)(?:\))
I would have gone for (reg.*) as a regular expression. but that doesn't work.
Solved! Go to Solution.
Much simpler regex, and the same output:
\((.*)\)
I think you should follow this @SylviaK
Hi,
I enjoy using RegEx. One of the many problems with RegEx can be incomprehensibility. If I can, I use Alteryx string functions as they are easier for others to understand.
Substring(Substring([Field], FindString([Field], '(') + 1),0,FindString(Substring([Field], FindString([Field], '(') + 1), ')'))
Something like this would do the same job. It locates the positions of the parentheses and grabs the text between them. Even though it is nested, it only takes a few minutes to unwind what is happening here.
As with everything in Designer, there are multiple ways to get to the same output.
In your case, (reg.*) does not work because the first line has a standalone "reg" value. The expression you wrote is looking for "reg" in any instance. Adding the ".*" means it can have 0 or more of any character.
If you wanted to include the "reg" portion in your regex statement, you could use (reg\w.*)\). The statement is looking for something that begins with "reg" followed by another letter. This will disregard the standalone "reg" value. Following, is anything else (represented by .*). The closing ")" is removed by having it outside of the market group. The ")" is a special character that needs to be signified by a "\" prior.
A more simple expression might be to just look for anything in the parenthesis, if you don't just need to find values that start with "reg." \((.*)\) will work in that case.