June 2000
Intermediate to advanced
556 pages
8h 45m
English
So far, we have performed regular expression matches by passing the required pattern into the search function provided by the re module.
As you can imagine, regular expressions can get quite complex. The bigger the regular expression, the more work the search function has to do to figure out what it means. The re module allows you to compile a regular expression into a regular expression object, as shown below.
CD-ROM reference=7063.txt
import re
EndTag = re.compile ("</(.+?)>")
f = open ('staff.xml','r')
for l in f.readlines():
mo = EndTag.search (l)
if mo:
print mo.group(1)
In the above example, the regular expression is parsed once during the call to compile. It is this compiled regular expression object ...
Read now
Unlock full access