Regular expression help

Alex Martelli aleax at aleax.it
Tue Apr 1 03:53:02 EST 2003


<posted & mailed>

Stephen Boulet wrote:

> How do I use the sub function to replace my match with my match
> prepended by a newline?

Simplest is to enlose your RE pattern in parentheses (making it
a group) and then use a backreference \1 in the substitution:

resulting_string = re.sub(r'(x.y.x)', r'\n\1', original_string)


Alex





More information about the Python-list mailing list