Entry
Is the match() function any different to having search() with a ^ at the beginning of the string?
May 30th, 2000 04:37
unknown unknown, Tim Peters
Yes, although the difference doesn't become clear until you exploit the
optional slicing arguments of compiled regexps:
>>> import re
>>> p = re.compile("^a")
>>> p.search("cba", 2)
>>> p = re.compile("a")
>>> p.match("cba", 2)
<re.MatchObject instance at ead590>
>>>
That is, while there is an "a" at index 2 of "cba", and "match" finds
it, a caret does *not* match starting at index 2 (caret means
start-of-string, and may or may not mean more than that depending on
"multiline" mode -- using "match" instead cuts all that magical crap out
of the equation).