[Tutor] returning the entire line when regex matches

Nick Burgess burgess.nick at gmail.com
Mon May 4 21:51:36 CEST 2009


Compiling the regular expression works great, I cant find the tutorial
Mr. Gauld is referring to!!  I searched python.org and alan-g.me.uk.
Does anyone have a link?



On Mon, May 4, 2009 at 1:46 PM, Martin Walsh <mwalsh at mwalsh.org> wrote:
> 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