Why doesn't this RE match?

John Machin sjmachin at lexicon.net
Fri Feb 6 19:55:41 EST 2009


On Feb 7, 11:23 am, "Just Another Victim of the Ambient Morality"
<ihates... at hotmail.com> 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?

Because that is exactly what it is documented to do.

> What am I missing?

Inter alia:
(1) The whole section of the fantastic manual devoted to this topic:
http://www.python.org/doc/2.6/library/re.html#matching-vs-searching

(2) a section in the fabulous HOWTO:
http://www.python.org/doc/2.6/howto/regex.html#performing-matches

(3) the re section in the phantasmagorical help sub-system (very handy
if your internet connection goes on the fritz):

Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> help(re)
Help on module re:
[snip]
    This module exports the following functions:
        match    Match a regular expression pattern to the beginning
of a string.
        search   Search a string for the presence of a pattern.
[snip]



More information about the Python-list mailing list