python – Difference between regex “(\d)(?=\d)\1” and “(\d)(?=\d\1)”?

It’s all about the lookahead which requires a forward match but does not advance the current location in the string.

The first regex matches repeating digits

(\d)        find a digit
(?=\d)      that is followed by a digit (positive lookahead)
\1          immediately followed by the original match

It’s the same as r"(\d)\1". The lookahead isn’t needed because the group match is already a digit.

The second regex matches a digit that is repeated 2 characters ahead

(\d)        find a digit
(?=\d\1)    that is followed by any digit and then the original match

It matches the first 5 in 515 because has the same digit twice with a single digit in between. The same with 494.

This second match only advanced one digit. Had you tested 51512249435, you would have gotten an extra match 51512249435.

Read more here: Source link