[Tutor] returning the entire line when regex matches

Martin Walsh mwalsh at mwalsh.org
Mon May 4 19:46:16 CEST 2009


Nick Burgess wrote:
> So far the script works fine, it avoids printing the lines i want and
> I can add new domain names as needed. It looks like this:
> 
> #!/usr/bin/python
> import re
> 
> outFile = open('outFile.dat', 'w')
> log = file("log.dat", 'r').read().split('Source') # Set the line delimiter
> for line in log:
>     if not re.search(r'notneeded.com|notneeded1.com',line):
>         outFile.write(line)

There is a subtle problem here -- the '.' means match any single
character. I suppose it's unlikely to bite you, but it could -- for
example, a line containing a domain named notneeded12com.net would
match. You should probably escape the dot, and while you're at it
compile the regular expression.

# untested
pattern = re.compile(r'notneeded\.com|notneeded1\.com')
for line in log:
    if not pattern.search(line):
        outFile.write(line)

HTH,
Marty



More information about the Tutor mailing list