Regular expression help
Raymond Hettinger
vze4rx4y at verizon.net
Tue Apr 1 00:45:53 EST 2003
"Stephen Boulet" <stephen at theboulets.net> wrote in message
news:2FGdnaUSmIhkghSjXTWc-g at speakeasy.net...
> How do I use the sub function to replace my match with my match
> prepended by a newline?
Make your match into a group by enclosing it in parenthesis.
Then, reference that group with a backslash and the group
number. If there is only one group, the group number is 1:
>>> import re
>>> s = 'the quick brown fox jumped over the lazy dog'
>>> print re.sub('(j....d)', r'\1\n', s)
the quick brown fox jumped
over the lazy dog
Raymond Hettinger
More information about the Python-list
mailing list