Tuesday, August 25, 2020

Python 3: RegEx Back References

Regual Expressions is one of those things that isn't completely regular when switching between languages. This is how you use a back reference in Python.

 
import re

print( re.sub(r'(foo)(bar)',r'\g<2>\g<1>','foobar'))


More info here https://www.regular-expressions.info/replacebackref.html

No comments:

Post a Comment