7.16. Current Implementation of xgrep

To wrap up this chapter, you may want to glance over the code for xgrep as it currently stands.

CD-ROM reference=7090.txt
C>type xgrep.py import strangest import re def XMLGrep(filename,pattern,MarkHits=0): Pat = re.compile (pattern) if MarkHits == 0: # Do not mark hits, make hit delimiters blank # strings. StartOfHit = "" EndOfHit = "" else: # Mark hits, make hit delimiters matching square # brackets. StartOfHit = "[" EndOfHit = "]" f = open (filename,"r") for L in f.readlines(): L = L[:-1] mo = Pat.search (l) if mo is not None: # Print out the match, using match delimiters # to show the "hit." print "%s:%s%s%s%s%s" % (filename, L[0:mo.start()], StartOfHit, L[mo.start():mo.end()], EndOfHit, L[mo.end():]) ...

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.