6.2 match() versus search()

The match() function only checks if the RE matches at the beginning of the string, while search() will scan forward through the string for a match. It's important to keep this distinction in mind. Remember, match() will only report a successful match which will start at 0; if the match wouldn't start at zero, match() will not report it.

>>> print re.match('super', 'superstition').span()  
(0, 5)
>>> print re.match('super', 'insuperable')    
On the other hand, search() will scan forward through the string, reporting the first match it finds.

>>> print re.search('super', 'superstition').span()
(0, 5)
>>> print re.search('super', 'insuperable').span()
(2, 7)
Sometimes you'll be tempted to keep using re.match(), and just add .* to the front of your RE. Resist this tempation, and use re.search() instead. The regular expression compiler does some analysis of REs in order to speed up the process of looking for a match. One such analysis figures out what the first character of a match must be; for example, a pattern starting with Crow must match starting with a "C". The analysis lets the engine quickly scan through the string looking for the starting character, and only try the full match if one is found.

Adding .* defeats this optimization, and requires scanning to the end of the string and then backtracking to find a match for the rest of the RE. Use re.search() instead.