Regular Expression Lookbehind doesn’t work with quantifiers (‘+’ or ‘*’)

Many regular expression libraries do only allow strict expressions to be used in look behind assertions like: only match strings of the same fixed length: (?<=foo|bar|\s,\s) (three characters each) only match strings of fixed lengths: (?<=foobar|\r\n) (each branch with fixed length) only match strings with a upper bound length: (?<=\s{,4}) (up to four repetitions) The … Read more

Python Regex Engine – “look-behind requires fixed-width pattern” Error

Python re lookbehinds really need to be fixed-width, and when you have alternations in a lookbehind pattern that are of different length, there are several ways to handle this situation: Rewrite the pattern so that you do not have to use alternation (e.g. Tim’s above answer using a word boundary, or you might also use … Read more

What’s the technical reason for “lookbehind assertion MUST be fixed length” in regex?

Lookahead and lookbehind aren’t nearly as similar as their names imply. The lookahead expression works exactly the same as it would if it were a standalone regex, except it’s anchored at the current match position and it doesn’t consume what it matches. Lookbehind is a whole different story. Starting at the current match position, it … Read more

Does lookaround affect which languages can be matched by regular expressions?

The answer to the question you ask, which is whether a larger class of languages than the regular languages can be recognised with regular expressions augmented by lookaround, is no. A proof is relatively straightforward, but an algorithm to translate a regular expression containing lookarounds into one without is messy. First: note that you can … Read more