Problem with REGEX in Python

Peter Hansen peter at engcorp.com
Fri May 30 07:30:23 EDT 2003


Martin P wrote:
> 
> 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.

The [] represents a character *set*, from which any character can
be matched.  It does not represent a *sequence*, with the matching
required to be in order.

You want something containing just "[pP]ython" most likely.




More information about the Python-list mailing list