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 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.