python - Zero or one quantifier (`?`) does not seem to be greedy -
given these 2 test strings:
'eitherxory.' 'justy.'
i match 'x' (or nothing, if 'x' not present) , 'y', respectively:
('x', 'y') (none, 'y')
the pattern i've come (x)?.*?(y)
matches are:
(none, 'y') (none, 'y')
what doing wrong?
i'm using python (import re; re.search(pattern, line).groups()
) question generic.
one option use:
(?:(x).*)?(y)
we want match .*
if have found x
, can group them , move optional quantifier outside. avoids case when .*
eats characters start of string.
keep in mind won't work if x
occurs after y
in string. use this:
(?=.*(x)).*(y)
Comments
Post a Comment