re.sub and named groups

Emanuele D'Arrigo manu3d at
Wed Feb 4 17:51:41 CET 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 :


instead, I was expecting:

"spam:foo ham:"

Why is that?

Thanks for your help!


More information about the Python-list mailing list