Regular Expressions: Can't quite figure this problem out

Miles semanticist at gmail.com
Mon Sep 24 21:27:19 EDT 2007


On 9/24/07, Robert Dailey <rcdailey at gmail.com> wrote:
> Hi,
>
> I'm attempting to create a regular expression that removes redundancy in
> empty XML elements. For example:
>
> <root></root>
>
> The regular expression would convert the XML above into:
>
>  <root/>

If you can guarantee that the XML is well-formed, then this should work:

pattern = r'<([^/>][^>]*(?<!/))></[^>]+>'
replace = r'<\1/>'

-Miles



More information about the Python-list mailing list