Regural expression puzzle for gurus

Carsten Gaebler clpy at snakefarm.org
Fri Jul 13 09:35:55 CEST 2001


Pekka Niiranen wrote:
>             original string:           xx:yy:#AAA.!:-#BBB:2324:#CCC:!"¤%
> 
>             after replacement:    xx:yy:#AAA#.!:-#BBB#:2324:#CCC#:!"¤%
> 
> How can I do replacement with a single regural expression line ?

>>> import re
>>> text = 'xx:yy:#AAA.!:-#BBB:2324:#CCC:!'
>>> re.sub(r'#\w+', lambda m: m.group(0)+'#', text)
'xx:yy:#AAA#.!:-#BBB#:2324:#CCC#:!'
>>> 

cg.



More information about the Python-list mailing list