infor.com
concierge
infor u
developer portal
documentation central
customer center
Posts
Categories
Groups
ERPs
CSD
FSM/HRT
LN
M3
Syteline/ CSI
Hubs
Developer
Healthcare
Hospitality
Public Sector
Onboarding
Release Center
CloudSuites
Aerospace & Defense (LN)
Automotive (LN)
Chemicals (M3)
Corporate (FSM/HRT)
Distribution (Sx.e/CSD)
Distribution Enterprise (M3)
Engineering & Construction (LN)
Fashion (M3)
Food & Beverage (M3)
Healthcare (FSM/HRT)
Industrial (Syteline/CSI)
Industrial Enterprise (LN)
Manufacturing (M3)
Public Sector (FSM/HRT)
Solutions
Supply Chain Management (SCM)
Human Capital Management (HCM)
Events
Groups
User Groups
Your Groups
About
Email Community Support
Infor News
Migrated Forums
Home
Clovertech
Clovertech Archive
Cloverleaf
Does TCL regexp "non-capturing" syntax work?
Peter Heggie
Given this string:
set s1 "<item>Provider/Practice : UHCC (UHCC Adult Care)</item>"
execute the regexp:
regexp {(?:.*Practice.*)\(.*\)(?:\</item\>)} "$s1" match
result is 1
examine the match contents:
echo $match
result:
<item>Provider/Practice : UHCC (UHCC Adult Care)</item>
The non-capturing tokens should remove all but "(UHCC Adult Care)", but I'm getting everything. How can I use non-capturing syntax in regexp?
Find more posts tagged with
Clovertech
Comments
Charlie Bursell
You are using positive look-ahead where you should be using negative (?!:)
The following returns: (UHCC Adult Care)
regexp {(?!:.*Practice.*)\(.*\)(?!:\</item\>)} "$s1" match
You could remove the parenthesis.
Look-ahead in regular expressions is tricky. If you want just the "UHC Adult Care" from this string I would write it like: Assumes you only what is inside parenthesis.
regexp -- {\((.*?)\)} $s1 {} match
Note the {} at the end throws away full match while match will contain UHCC Adult Care
I use look-ahead only when forced to :=)
Peter Heggie
Thank you! you are right - I do not know how to use look-ahead - I will use your syntax..
I usually just muddle through regular expressions.. but often I hit land mines.
Peter
Jim Kosloskey
Seems to me this could be done with string functions.
Peter Heggie
Jim, yes this could have been done with string functions; I'm saving three or four lines of code by using regular expressions. It is a trade-off when considering everyone's skill set vs. learning and writing more with regular expressions. Right now I need to get better at regex because we have another product that parses and returns data from PDFs based on regular expressions, but I also just want to know regex better.
Charlie, I got the regex to work using your negative look-ahead example. I also used your syntax to remove the parenthesis by matching on the opening and closing parens, but then throwing out all but the (second) matching string (I needed the additional beginning and ending text strings because parenthesis can also be found in other areas of the discharge instructions):
given string s2 of:
<item>Provider/Practice : SGI, Syracuse Gastroenterology (Syracuse Gastroenterology)</item>
regex of:
regexp {(?!:<item>Provider/Practice.*)\((.*?)\)(?!:.*item>)} "$s2" {} match
returns 1 and $match = Syracuse Gastroenterology
Thank you
Charlie Bursell
If you really want to use look-ahead/look behind remember they create a *lot* of overhead and are arcane to the casual user that may have to maintain this later.
I prefer to keep it simple like:
regexp -- {<item>Provider/Practice.*?\((.*?)\)} $s2 {} match
If doing something like you did at least provide comments as exactly what you are doing or use the expanded switch with the regex to explain
Example:
set x
http://www.infor.com
regexp -expanded -- {
^ # beginning of string
[^:]+ # all characters to the first colon
(?= # begin positive lookahead
.*\.com$ # for a trailing .com
) # end positive lookahead
} $x match
<b>=> </b>1 <b>
</b> echo $match
=> http
Is much better understood than:
regexp {^[^:]+(?=.*\.com$)} $x match
=> 1
echo $match
=> http
Peter Heggie
ok I can see what you are doing with removing the non-capturing components - as long as the only 'group', in parenthesis, is the text I want, then by using the null {} return-variable specifier, then only the group(s) are returned, each going to their own specified (sub) variable. and the first subvar ( 'match') points to the first group captured (the only group).
Why is the question mark there? I thought it specified that the preceding character or group was optional? Apparently I need it in both places, otherwise the group fails to match.
Jim Kosloskey
Peter,
I was not implying that string functions over regexp but rather making sure anyone following the post were aware this could be accomplished not using regexp should they not feel comfortable using regexp.
Peter Heggie
Jim - sure, understood. Actually I use a lot of string functions all over the place - I can pretty much build anything I want with them. I wrote a crude XML parser with string functions (talk about memory leaks!) and still use some for CCDA processing. In this situation, I thought I could use less code. It also happens to include a 'start position' option and an 'indices' option to tell me where in the text block my string is, which saves a few more lines also as I iterate through the text block looking for the next string match.
Charlie Bursell
The question mark after .* makes the match non-greedy in that it will find the first match. By default, the match is always greedy which means it will make the maximum match. In your case, since there is only one set of parenthesis it was not really needed.
Warning: do not mix greedy/non-greedy it can return weird results
In the examples below the first RE is greedy, and the second is non-greedy:
set x {
He
sits, but
she
stands.}
regexp -- {
.*
} $x match; echo $match
=>
He
sits, but
she
regexp -- {
.*?
} $x match; echo $match
=>
He
FYI: there are many regular expression tutorials available in Youtube.