Problem with REGEX in Python

Nick Vargish nav at adams.patriot.net
Fri May 30 09:42:50 EDT 2003


"Martin P" <martin_p at despam.com> writes:

> p = re.compile('.*[pP][ython].*')
> f=file("aufgabe14.test")
> for currentline in f:
>  if p.match(currentline):
>   print currentline

As others have said, you probably want p = re.compile('.*[Pp]ython.*').

You could also consider using p.search(currentline) instead of
match. This would allow you to use '[Pp]ython' without the leading
'.*' portion of the regular expression.

r.match(s) is implicitly anchored to the start of s, whereas
r.search(s) is not.

Nick

-- 
# sigmask.py  ||  version 0.2  ||  2003-01-07  ||  Feed this to your Python.
print reduce(lambda x,y:x+chr(ord(y)-1),'Ojdl!Wbshjti!=obwAqbusjpu/ofu?','')





More information about the Python-list mailing list