1. 程式人生 > 實用技巧 >What is the difference between re.search and re.match?

What is the difference between re.search and re.match?

What is the difference between re.search and re.match?

re.matchis anchored at the beginning of the string. That has nothing to do with newlines, so it is not the same as using^in the pattern.

As there.match documentationsays:

If zero or more characters at thebeginning of stringmatch the regular expression pattern, return a correspondingMatchObject

instance. ReturnNoneif the string does not match the pattern; note that this is different from a zero-length match.

Note: If you want to locate a match anywhere in string, usesearch()instead.

re.searchsearches the entire string, asthe documentation says:

Scan through stringlooking for a location where the regular expression pattern produces a match, and return a correspondingMatchObject

instance. ReturnNoneif no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.

So if you need to match at the beginning of the string, or to match the entire string usematch. It is faster. Otherwise usesearch.

The documentation has a

specific section formatchvs.searchthat also covers multiline strings:

Python offers two different primitive operations based on regular expressions:matchchecks for a matchonly at the beginningof the string, whilesearchchecks for a matchanywherein the string (this is what Perl does by default).

Note thatmatchmay differ fromsearcheven when using a regular expression beginning with'^':'^'matches only at the start of the string, or inMULTILINEmode also immediately following a newline. The “match” operation succeedsonly if the pattern matches at thestartof the stringregardless of mode, or at the starting position given by the optionalposargument regardless of whether a newline precedes it.

Now, enough talk. Time to see some example code:

# example code:
string_with_newlines = """something
someotherthing"""

import re

print re.match('some', string_with_newlines) # matches
print re.match('someother', 
               string_with_newlines) # won't match
print re.match('^someother', string_with_newlines, 
               re.MULTILINE) # also won't match
print re.search('someother', 
                string_with_newlines) # finds something
print re.search('^someother', string_with_newlines, 
                re.MULTILINE) # also finds something

m = re.compile('thing$', re.MULTILINE)

print m.match(string_with_newlines) # no match
print m.match(string_with_newlines, pos=4) # matches
print m.search(string_with_newlines, 
               re.MULTILINE) # also matches