[Tutor] returning the entire line when regex matches
Alan Gauld
alan.gauld at btinternet.com
Sun May 3 23:04:51 CEST 2009
"Nick Burgess" <burgess.nick at gmail.com> wrote
> How do I make this code print lines NOT containing the string 'Domains'?
>
Don't use regex, use in:
for line in log:
if "Domains" in line:
print line
> This does not work...
>
> if re.search != (r'Domains:', line):
Because you are assigning a tuple containing a string and a line
of text to a name which is currently bound to a function object
(re.search) which is not a sensible thing to do since you lose access
to the search function. If you really do need to use regex you probably
want:
if not re.search(r'Domains', line):
print line
But if it is a simple string you are searching for regex is overkill
and probably slower than using in.
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list