0

From Python 3.4.1 docs:

(?=...)

Positive lookahead assertion. This succeeds if the contained regular expression, represented here by ..., successfully matches at the current location, and fails otherwise. But, once the contained expression has been tried, the matching engine doesn’t advance at all; the rest of the pattern is tried right where the assertion started.

I'm trying to understand regex in Python. Could you please help me understand the second sentences, especially the bolded words? Any example will be appreciated.

2
  • There are already some great resources on SO and outside for regex help in understanding Jul 2, 2014 at 4:21
  • It means that the regex engine, once reached: (?=...) stops consuming the string - but, it keeps looking forward through the expression looking to match the ... in (?:...)
    – Nir Alfasi
    Jul 2, 2014 at 4:24

3 Answers 3

6

Lookarounds are zero-width assertions. They don't consume any characters on the string.

To touch briefly on the bolded portions of the documentation:

This means that after looking ahead, the regular expression engine is back at the same position on the string from where it started looking. From there, it can start matching again...

The key point:

You can get a zero-width match which is a match that does not consume any characters. It only matches a position in the string. The point of zero-width is the validation to see if a regular expression can or cannot be matched looking ahead or looking back from the current position, without adding them to the overall match.

0
3

An answer in an example form. On string "xy":

  • (?:x) will match "x"
  • (?:x)x will not match, because there is no another x after x
  • (?:x)y will match "xy", by advancing over x and then y.

  • (?=x) will match "" at the start of the string, since x is following.

  • (?=x)x will match "x" - it recognises that an x follows, and then it advances over it.
  • (?=x)y will not match, since it affirms there is an x following, but then tries to advance over it using y.
1
  • 1
    To clarify, ?: is noncapturing group and ?= is positive lookahead. Sep 5, 2023 at 16:10
2

Generally a Regular Expression engine is "consuming" your string character by character as it matches up with your regular expression.

If you use a look-ahead operator, the engine will instead simply look ahead without "consuming" any characters while it looks for a match.

Example

A good example is a regular expression to match a password where it needs to have a single numeric digit as well as be between 6-20 characters long.

You could write two checks (one to check if a digit exists, and one to check if the string length is as required), or use a single regular expression:

(?=.*\d).{6,20}

The first portion (?=.*\d)checks if there is digit anywhere in the string. When it completes we are back at the beginning of the string again (we were only "looking-ahead") and if it passed, we go onto the next portion of the regex.

Now .{6,20} is no longer a lookahead, and begins consuming the string. When the entire string is consumed, a match has been found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.