The Question :
555 people think this question is useful
What is the difference between the
match() functions in the Python
I’ve read the documentation (current documentation), but I never seem to remember it. I keep having to look it up and re-learn it. I’m hoping that someone will answer it clearly with examples so that (perhaps) it will stick in my head. Or at least I’ll have a better place to return with my question and it will take less time to re-learn it.
The Question Comments :
The Answer 1
540 people think this answer is useful
re.match is 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 the re.match documentation says:
If zero or more characters at the
beginning of string match the regular expression pattern, return a
None if 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, use
re.search searches the entire string, as the documentation says:
Scan through string looking for a
location where the regular expression
pattern produces a match, and return a
None if no position in the
string matches the pattern; note that
this is different from finding a
zero-length match at some point in the
So if you need to match at the beginning of the string, or to match the entire string use
match. It is faster. Otherwise use
The documentation has a specific section for
search that also covers multiline strings:
Python offers two different primitive
operations based on regular
match checks for a match
only at the beginning of the string,
search checks for a match
anywhere in the string (this is what
Perl does by default).
match may differ from
even when using a regular expression
'^' matches only
at the start of the string, or in
MULTILINE mode also immediately
following a newline. The “
operation succeeds only if the pattern
matches at the start of the string
regardless of mode, or at the starting
position given by the optional
argument regardless of whether a
newline precedes it.
Now, enough talk. Time to see some example code:
# example code:
string_with_newlines = """something
print re.match('some', string_with_newlines) # matches
string_with_newlines) # won't match
print re.match('^someother', string_with_newlines,
re.MULTILINE) # also won't match
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
re.MULTILINE) # also matches
The Answer 2
107 people think this answer is useful
search ⇒ find something anywhere in the string and return a match object.
match ⇒ find something at the beginning of the string and return a match object.
The Answer 3
53 people think this answer is useful
match is much faster than search, so instead of doing regex.search(“word”) you can do regex.match((.*?)word(.*?)) and gain tons of performance if you are working with millions of samples.
This comment from @ivan_bilan under the accepted answer above got me thinking if such hack is actually speeding anything up, so let’s find out how many tons of performance you will really gain.
I prepared the following test suite:
LENGTH = 10
LIST_SIZE = 1000000
word = [random.choice(string.ascii_lowercase) for _ in range(LENGTH)]
word = ''.join(word)
wordlist = [generate_word() for _ in range(LIST_SIZE)]
start = time.time()
[re.search('python', word) for word in wordlist]
print('search:', time.time() - start)
start = time.time()
[re.match('(.*?)python(.*?)', word) for word in wordlist]
print('match:', time.time() - start)
I made 10 measurements (1M, 2M, …, 10M words) which gave me the following plot:
The resulting lines are surprisingly (actually not that surprisingly) straight. And the
search function is (slightly) faster given this specific pattern combination. The moral of this test: Avoid overoptimizing your code.
The Answer 4
50 people think this answer is useful
re.search searches for the pattern throughout the string, whereas
re.match does not search the pattern; if it does not, it has no other choice than to match it at start of the string.
The Answer 5
33 people think this answer is useful
You can refer the below example to understand the working of
re.match and re.search
a = "123abc"
t = re.match("[a-z]+",a)
t = re.search("[a-z]+",a)
re.match will return
re.search will return
The Answer 6
31 people think this answer is useful
The difference is,
re.match() misleads anyone accustomed to Perl, grep, or sed regular expression matching, and
re.search() does not. 🙂
More soberly, As John D. Cook remarks,
re.match() “behaves as if every pattern has ^ prepended.” In other words,
re.search('^pattern'). So it anchors a pattern’s left side. But it also doesn’t anchor a pattern’s right side: that still requires a terminating
Frankly given the above, I think
re.match() should be deprecated. I would be interested to know reasons it should be retained.
The Answer 7
15 people think this answer is useful
re.match attempts to match a pattern at the beginning of the string. re.search attempts to match the pattern throughout the string until it finds a match.
The Answer 8
7 people think this answer is useful
Following Ex says it:
>>> a = "123abc"