June 2000
Intermediate to advanced
556 pages
8h 45m
English
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():]) ...Read now
Unlock full access