faqts : Computers : Programming : Languages : Python : Language and Syntax

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 4 people (75%) answered Yes
Recently 1 of 2 people (50%) answered Yes

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