By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,537 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,537 IT Pros & Developers. It's quick & easy.

Re: Python regex question

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.