Problem with REGEX in Python

Peter Abel p-abel at t-online.de
Fri May 30 15:58:59 EDT 2003


Nick Vargish <nav at adams.patriot.net> wrote in message news:<yyy3ciwr1np.fsf at adams.patriot.net>...
> "Martin P" <martin_p at despam.com> writes:
> 
> > p = re.compile('.*[pP][ython].*')
> > f=file("aufgabe14.test")
**Here f is a filepointer**

> > for currentline in f:
**So this code can't work, because you can't iterate over a filepointer

> >  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

A further possibility would be *findall*, wich returns all lines, where
*search* would hit.

>>> py="""<aufgabe14.test>
... Das ist eine Python Beispielzeile
... eine andere PyTHOn Beispielseite
... bla
... bla Python
... asdlasdlda
... ython
... und wieder python python PyhthoN pyton pyTon
... """
>>> p=re.compile('.*?[pP]ython.*')
>>> # ** I prefer .*?, because it's not greedy
>>> lines_found=p.findall(py)
>>> print '\n'.join(lines_found)
Das ist eine Python Beispielzeile
bla Python
und wieder python python PyhthoN pyton pyTon
>>> 

Regards
Peter




More information about the Python-list mailing list