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

1
  • 3
    ^ is zero width match and Python regex engine doesn't support alternation of zero with and non-zero alternations in lookbehind. Commented Oct 8, 2020 at 14:42

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.