Regular Expressions to Replace Strings

Alex Martelli aleaxit at yahoo.com
Fri May 18 08:53:07 EDT 2001


<piet at cs.uu.nl> wrote in message news:uitiygb6y.fsf at cs.uu.nl...
> >>>>> Andrew Kuchling <akuchlin at mems-exchange.org> (AK) writes:
>
> AK> Group 0 corresponds to the match as a whole, so you could use \0 in
> AK> your replacement pattern.
>
> In my python 2.0 \0 doesn't work. It just inserts a \0 character.

True (in both 2.0 and 2.1, it seems) -- you appear to need
the alternative \g<0> syntax, for some reason... this works:

import re

base = 'tanto va la gatta al lardo'
vow = re.compile('a|o')
resu = vow.sub(r'"\g<0>"', base)
print resu


Alex






More information about the Python-list mailing list