0

In Python's regex: why does regex1 complain about fixed-width look-behinds if regex2 does not? Is ^ (start marker) more than 1 character?

Regex1

(?<=^|b)[0-9]

Regex2

(?<=a|b)[0-9]

Reproduceable in https://regex101.com/r/L5J47R/2

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
fersarr
  • 3,399
  • 3
  • 28
  • 35
  • 3
    `^` is zero width match and Python regex engine doesn't support alternation of zero with and non-zero alternations in lookbehind. – anubhava Oct 08 '20 at 14:42

1 Answers1

1

Reason why first regex is nor working in Python because ^ is a zero width match and Python regex engine doesn't support alternation of zero with and non-zero alternations in the lookbehind assertion.

This is however supported in other engines such as Java, PHP, Perl, C# etc.

To solve this problem, you can use this regex:

(?:^|(?<=b))[0-9]

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643