Why doesn't this RE match?

  • Thread starter Just Another Victim of the Ambient Morality
  • Start date
J

Just Another Victim of the Ambient Morality

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?
Thank you...
 
V

Vlastimil Brom

2009/2/7 Just Another Victim of the Ambient Morality said:
   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?
   Thank you...

Try re.search() instead of match(), if nod only the beginning should
be checked for a match:
see
http://docs.python.org/library/re.html#matching-vs-searching

vbr
 
M

MRAB

Just said:
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?
Thank you...
match() is anchored at (ie matches only at) the start of the string. You
need search(). It's in the docs! :)
 
S

Stephen Hansen

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?

You want regex.search(). match specifically looks for the pattern from
the start of the screen, search anywhere.

--S
 
R

Robert Kern

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
 
J

John Machin

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top