Not able to understand python regex with ?=

python, regex

Solution

`(?=...)` is a positive lookahead assertion. It matches if there is the part in parentheses after `?=` matches at the current position, but it will not consume any characters for the match. E.g. the regex `a(?=b)` will match an `a` followed by a `b`, but won't return the `b` as part of the match.

`(?<=...)` is the same, but a look behind, i.e. it looks backwards. Again, it doesn't consume anything.

`(?(id/name)yes-pattern|no-pattern)` is a conditional. If the named group `id/name` matched, then the string must match `yes-pattern` at this point, otherwise `no-pattern`.

To be honest, though, those are quite advanced features and I cannot remember ever having used a conditional. Lookaround is more common, but often very constrained by the regex engines, e.g. lookbehind can only be done with fixed-length strings in many cases.

Problem

I am not able to understand how these regex works and when they are used. I haven't found concrete examples of these regex on python website. I know sed awk but haven't used these type of regex there ``` (?=...) (?<=...) (?(id/name)yes-pattern|no-pattern) ```

Original source