[Tutor] Example of use of (?P<name>) and (?P=name) in Python regular expressions?
Michael Fourman
michael.fourman at ed.ac.uk
Wed Jan 6 12:40:39 CET 2010
I've used (?P=name) recently in an implementation of the porter2 stemming
algorithm from http://snowball.tartarus.org/algorithms/english/stemmer.html
This includes the rule:
## if the word ends with a double remove the last letter (so hopp -> hop)
where we
## Define a double as one of
## bb dd ff gg mm nn pp rr tt
re.sub("^(?P<stem>.*?[aeiouy].*?(?P<dd>[bdfgmnprt]?))(?P=dd)$", "\g<stem>",
word)
implements this rule.
Best,
Michael
Michael Hannon-2 wrote:
>
> Greetings. While looking into the use of regular expressions in Python, I
> saw that it's possible to name match groups using:
>
> (?P<name>...)
>
> and then refer to them using:
>
> (?P=name)
>
> 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)
> >>> y
> '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?
>
> Thanks.
>
> -- Mike
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
View this message in context: http://old.nabble.com/Example-of-use-of-%28-P%3Cname%3E%29-and-%28-P%3Dname%29-in-Python-regular-expressions--tp26557967p27026833.html
Sent from the Python - tutor mailing list archive at Nabble.com.
More information about the Tutor
mailing list