[Tutor] returning the entire line when regex matches

Steve Willoughby steve at alchemy.com
Sun May 3 22:48:34 CEST 2009


Nick Burgess wrote:
> How do I make this code print lines NOT containing the string 'Domains'?
> 
> 
> import re
> for line in log:
>     if re.search (r'Domains:', line):
>         print line
> 
> 
> This does not work...
> 
> if re.search != (r'Domains:', line):

re.search (r'Domains:', line)

is a function call which (simplifying slightly here) returns a true
value if <line> matches the regex "Domains:".  It might make it more
clear if you leave out the extra space there (because I think you're
getting confused thinking re.search and (...) are two separate
expressions since you put != between them):

re.search(r'Domains:', line)

If you want to print lines which do NOT match, try this:

if not re.search(r'Domains:', line):
   print line




More information about the Tutor mailing list