Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with regex backreference in Python

I am not sure why this is not working. Perhaps I am missing something with Python regex.

Here is my regex and an example string of what I want it to match too:

    PHONE_REGEX        = "<(.*)>phone</\1>"
    EXAMPLE            = "<bar>phone</bar>"

I tested this match in isolation and it failed. I used an online regex tester and it matched. Am I simply missing something that is particular to Python regex?

Thanks!

like image 347
PFranchise Avatar asked May 04 '26 12:05

PFranchise


1 Answers

You have to mark the string as a raw string, due to the \ in there, by putting an r in front of the regex:

m = re.match(r"<(.*)>phone</\1>", "<bar>phone</bar>")
like image 198
Simeon Visser Avatar answered May 07 '26 02:05

Simeon Visser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!