7.6. A Pattern-Matching Dry Run

Before we delve into regular expressions, it would be useful to have a functioning xgrep that can search for plain vanilla strings in the files specified on the command line. Doing this much enables us test the end-to-end operation of xgrep. We are nearly there; all we need is to beef up the XMLGrep function a little bit. The modified XMLGrep function is shown below.

 CD-ROM reference=7042.txt import string def XMLGrep(filename,pattern): # for the moment, search the specified file, treating # the pattern argument as a # straight string i.e., not as a regular expression f = open (filename,"r") for l in f.readlines(): pos = string.find (l,pattern) if pos != -1: # print out the match, using square # brackets to show ...

Get XML Processing with Python now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.