Reputation: 471
set string {rose is an flower jasmine is a flower rose is an flower rose is a colour jasmine is a flower}
From the above string I want to match rose is an flower
and rose is a colour
.
So I did regexp like below,
% puts [regexp -all {rose.+?(flower|colour)} $string]
3
from the above o/p we had three matches.
% puts [regexp -all -inline {rose.+?(flower|colour)} $string]
{rose is an flower} flower {rose is an flower} flower {rose is a colour} colour
From the above output am expecting:
{rose is an flower} {rose is an flower} {rose is a colur}
I don't know how the below highlighted words are coming
{rose is an flower} flower {rose is an flower} flower {rose is a colour} colour
^^^^^^ ^^^^^^ ^^^^^^
If someone explains, I would be grateful.
Upvotes: 0
Views: 158
Reputation: 41838
Watch out with Lazy Quantifiers in Tcl
Tcl lazy quantifiers are unreliable when followed by an alternation because they want to return the longest match (so they may jump further than you expect). Use this regex instead:
rose(?:(?!flower|colour).)*(?:flower|colour)
See this demo.
Upvotes: 1
Reputation: 17176
Subexpressions surrounded by parentheses ()
define capture groups that will be extracted for you separately. From the manuals:
(regular expression)` Parenthesis surrounding one ore more regular expressions specify a nested regular expression or choice of several regular expressions. The substring matching the nested regular expression is captured and can be referred to via the back reference mechanism, and alo captured into the corresponding match variable specified as an arbument to the command.
If you do not want to extract a capture group, you just add ?:
at the beginning of the parentheses, in which case they'll just define a sub expression:
%puts [regexp -all -inline {rose.+?(?:flower|colour)} $string]
{rose is an flower} {rose is an flower} {rose is a colour}
Upvotes: 4