Regex problem

Andrew Dalke dalke at dalkescientific.com
Mon Oct 15 16:31:39 EDT 2001


Gustaf Liljegren:
>It works fine when
 ...
>>>> re.match(re_link, s1).group()
 ...
>look what happens as soon as I add a space (or any other character)
>before:
 ...
>What's wrong here? Matches shouldn't have to start from the beginning of a
>string.

Why not?  The documentation for 'match' says:

http://www.python.org/doc/current/lib/re-objects.html
> If zero or more characters at the beginning of string match this
> regular expression, return a corresponding MatchObject instance.

You want the 'search' method.

You're not the only one who runs into this confusion.  Even though
I've been doing Python for years now, my old Perl habits still
want to call it "match" instead of "search".  That's documented too:
  http://www.python.org/doc/current/lib/matching-searching.html

                    Andrew
                    dalke at dalkescientific.com







More information about the Python-list mailing list