Jir
Jir

Reputation: 3155

Zero or one quantifier (`?`) does not seem to be greedy

Given these two test strings:

'eitherxory.'
'justy.'

I would like to match 'x' (or nothing, if 'x' is not present) and 'y', respectively:

('x', 'y')
(None, 'y')

The pattern I've come up with is (x)?.*?(y) but the matches are:

(None, 'y')
(None, 'y')

What am I doing wrong?

I'm using Python (import re; re.search(pattern, line).groups()) but the question is actually generic.

Upvotes: 2

Views: 260

Answers (1)

grc
grc

Reputation: 23575

One option is to use:

(?:(x).*)?(y)

We only want to match .* if we have found x, so we can group them together and move the optional quantifier outside. This avoids the case when .* eats up all the characters from the start of the string.

Keep in mind that this won't work if x occurs after y in the string. For that you could use something like this:

(?=.*(x)).*(y)

Upvotes: 2

Related Questions