[Python-Dev] Discordance in documentation...

Bob Halley halley at play-bow.org
Fri Sep 5 01:15:35 EDT 2003


gminick <gminick at hacker.pl> writes:

> import re
> a = "Fuentes Rushdie Marquez"
> print re.search("Rushdie|Fuentes", a).group() # returns "Fuentes"
> 
> According to the documentation I suspected it will return "Rushdie" 
> rather than "Fuentes", but it looks like it returns first part of the
> string that matches rather than first part of regular expression.

You are assuming that

        re.search("Rushdie|Fuentes", a)

is evaluated like

        m = re.search("Rushdie", a)
        if m is None:
            m = re.search("Fuentes", a)

But it doesn't work that way.  It works more like:

        m = None
        for i in xrange(0, len(a)):
            s = a[i:]
            m = re.match("Rushdie|Fuentes", s)
            if not m is None:
                break

I.e., try to match the expression at the start of the string; if that
doesn't work, try matching at the next character, etc.



More information about the Python-Dev mailing list