re.sub and named groups

Emanuele D'Arrigo manu3d at gmail.com
Wed Feb 4 11:51:41 EST 2009


Hi everybody,

I'm having a ball with the power of regular expression but I stumbled
on something I don't quite understand:

theOriginalString = "spam:(?P<first>.*) ham:(?P<second>.*)"
aReplacementPattern = "\(\?P<first>.*\)"
aReplacementString= "foo"
re.sub(aReplacementPattern , aReplacementString, theOriginalString)

results in :

"spam:foo"

instead, I was expecting:

"spam:foo ham:"

Why is that?

Thanks for your help!

Manu







More information about the Python-list mailing list