Problem with REGEX in Python

Martin P martin_p at despam.com
Fri May 30 07:34:31 EDT 2003


Hello,

for a short exercise I have to write a Python-Skript which reads different
lines from a file and prints out all lines which contain the words "Python"
or "python" but not f.e. "pyTHon".

I´ve build it like this:
p = re.compile('.*[pP][ython].*')

But it does not really work, because my it also offers "pyTHon" as a
solution.



Here´s the whole programm

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


Here my example file:

<aufgabe14.test>
Das ist eine Python Beispielzeile
eine andere PyTHOn Beispielseite
bla
bla Python
asdlasdlda
ython
und wieder python python PyhthoN pyton pyTon
</aufgabe14.test>

and here the output:

<output>
Das ist eine Python Beispielzeile
eine andere PyTHOn Beispielseite
bla Python
und wieder python python PyhthoN pyton pyTon
</output>

As you see, the programm also outputs:
"eine andere PyTHOn Beispielseite"
but it should not do it.

Thanks for help,

bye,

Martin







More information about the Python-list mailing list