472,143 Members | 1,155 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

Re: Python regex question

Tim van der Leeuw wrote:
Hi,

I'm trying to create a regular expression for matching some particular
XML strings. I want to extract the contents of a particular XML tag,
only if it follows one tag, but not follows another tag. Complicating
this, is that there can be any number of other tags in between. [...]
Sounds like this would be easier to implement using Python's SAX API.

Here's a short example that does something similar to what you want to
achieve:

import xml.sax

test_str = """
<xml>
<ignore/>
<foo x="1" y="2"/>
<noignore/>
<foo x="3" y="4"/>
</xml>
"""

class MyHandler(xml.sax.handler.ContentHandler):
def __init__(self):
xml.sax.handler.ContentHandler.__init__(self)
self.ignore_next = False

def startElement(self, name, attrs):
if name == "ignore":
self.ignore_next = True
return
elif name == "foo":
if not self.ignore_next:
# handle the element you're interested in here
print "MY ELEMENT", name, "with", dict(attrs)

self.ignore_next = False

xml.sax.parseString(test_str, MyHandler())

In this case, this looks much clearer and easier to understand to me
than regular expressions.

-- Gerhard

Jun 27 '08 #1
0 631

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Michael McGarry | last post: by
5 posts views Thread by Vamsee Krishna Gomatam | last post: by
3 posts views Thread by gisleyt | last post: by
10 posts views Thread by Raymond | last post: by
3 posts views Thread by Walter Cruz | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.