jfsturtz
jfsturtz

Reputation: 437

What does following a lookahead with a quantifier do?

Just trying to wrap my mind around this question, which occurred to me while I was messing around with positive lookaheads.

Does this regex make any sense?

foo(?=bar)+

re.match() doesn't return an error, but if there's any sense to the + quantifier, I can't figure what it would be. (FWIW, regex101.com gives the error 'The preceding token is not quantifiable')

Upvotes: 1

Views: 213

Answers (1)

gyre
gyre

Reputation: 16777

There is no reason to use the + quantifier here. Regular expression lookaheads and lookbehinds don't actually match any text, which means that if they match once in a position they will "match" an infinite number of times in a row. It seems python is smart enough not to try matching the lookahead more than once, as it does not get caught up in an infinite loop.

In other words, just stick with unqualified lookaheads: foo(?=bar) is best.

Upvotes: 1

Related Questions