Regular Expressions: Can't quite figure this problem out

Robert Dailey rcdailey at gmail.com
Mon Sep 24 21:53:57 EDT 2007


Thanks.

Any easier way to do a 'replace' then using start(), end(), and slicing
operations? I'm currently doing it manually.

On 9/24/07, Miles <semanticist at gmail.com> wrote:
>
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070924/75331f7a/attachment.html>


More information about the Python-list mailing list