Regular Expressions: Can't quite figure this problem out

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Sep 25 22:49:22 EDT 2007


En Tue, 25 Sep 2007 18:21:08 -0300, Robert Dailey <rcdailey at gmail.com>  
escribi�:

> One thing I noticed is that it is placing an arbitrary space between "  
> and
> />. For example:
>
>
> <root><frame type="image" /></root>
>
> Notice that there's a space between "image" and /></root>

Just remove the space in the replacement string:

>> py> print re.sub(r"<(\w+)([^>]*)></\1>", r"<\1\2 />", source)
                                                    ^
                                                    |

-- 
Gabriel Genellina




More information about the Python-list mailing list