Why doesn't this RE match?

Robert Kern robert.kern at gmail.com
Fri Feb 6 19:38:16 EST 2009


On 2009-02-06 18:23, Just Another Victim of the Ambient Morality wrote:
>      I'm confused by this behaviour:
>
>
> import re
>
> regex = re.compile('foo')
> match = regex.match('whatfooever')
>
>
>      In my experience with regular expressions, regex should have found a
> match.  However, in this case regex.match() returns None.  Why is that?
> What am I missing?

http://docs.python.org/library/re#re.RegexObject.match

Basically, regex.match() tries to apply the regexp to the beginning of the 
string. Use regex.search() to search for a match later in the string.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list