0

The url of my username is:

https://stackoverflow.com/users/12283851/user12283851

For this username it looks like the regular expression might be close to:

r'https?://stackoverflow.com/users/\d{1,9}/user\d{1,9}'

Is there a way in the regex to make sure that the first ID matches the second? In other words:

https://stackoverflow.com/users/12283851/user12283851 <== Valid
https://stackoverflow.com/users/11111111/user12283851 <== Invalid

1 Answers1

0

This is accomplished by using backreferences.

The backreference \1 (backslash one) references the first capturing group. \1 matches the exact same text that was matched by the first capturing group

In your example the following regex would work:

https?://stackoverflow\.com/users/(\d{1,9})/user\1

See this demo

Pedro Pinheiro
  • 1,059
  • 14
  • 32