[Tutor] Example of use of (?P<name>) and (?P=name) in Python regular expressions?
jm_hannon at yahoo.com
Sun Nov 29 00:15:36 CET 2009
Greetings. While looking into the use of regular expressions in Python, I saw that it's possible to name match groups using:
and then refer to them using:
I was able to get this to work in the following, nonsensical, example:
>>> x = 'Free Fri Fro From'
>>> y = re.sub(r'(?P<test>\bFro\b)', r'Frodo (--matched from \g<test>)', x)
'Free Fri Frodo (--matched from Fro) From'
But, as you can see, to refer to the match I used the "\g" notation (that I found some place on the web).
I wasn't able to find a way to use the "P=" syntax, and I wasn't able to find any working examples of this syntax on the web.
If you have a working example of the use of the "P=" syntax, will you please send it to me?
More information about the Tutor