简体   繁体   中英

Why have re.match()?

I know this topic has already been discussed multiple times here on StackOverflow, but I'm looking for a better answer.

While I appreciate the differences , I was not really able to find a definitive explanation of why the re module in python provides both match() and search() . Couldn't I get the same behavior with search() , if I prepend ^ in single line mode, and /A in multiline mode? Am I missing anything?

I tried to understand the implementation looking at the _sre.c code and I understand that the search ( sre_search() ) is actually implemented moving the pointer in the string to be searched, and applying the sre_match() on it, until a match is found.

So I guess that using the re.match() might be slightly faster than the corresponding regular expression (with ^ or /A ) using the re.search() . Is that the reason?

I also researched the python-dev ML archives but to no avail.

>>> string="""first line
... second line"""
>>> print re.match('first', string, re.MULTILINE)
<_sre.SRE_Match object at 0x1072ae7e8>
>>> print re.match('second', string, re.MULTILINE)
None
>>> print re.search('\Afirst', string, re.MULTILINE)
<_sre.SRE_Match object at 0x1072ae7e8>
>>> print re.search('\Asecond', string, re.MULTILINE)
None

As you already know, re.match will test the pattern only at the start of the string and re.search will test all the string until it find a match.

So, is there a difference between re.match('toto', s) and re.search('^toto', s) and what it is?

Lets make a little test:

#!/usr/bin/python

import time
import re

p1 = re.compile(r'toto')
p2 = re.compile(r'^toto')

ssize = 1000

s1 = 'toto abcdefghijklmnopqrstuvwxyz012356789'*ssize
s2 = 'titi abcdefghijklmnopqrstuvwxyz012356789'*ssize

nb = 1000

i = 0
t0 = time.time()
while i < nb:
    p1.match(s1)
    i += 1
t1 = time.time()

i = 0
t2 = time.time()
while i < nb:
    p2.search(s1)
    i += 1
t3 = time.time()

print "\nsucceed\nmatch:"
print (t1-t0)
print "search:"
print (t3-t2)


i = 0
t0 = time.time()
while i < nb:
    p1.match(s2)
    i += 1
t1 = time.time()

i = 0
t2 = time.time()
while i < nb:
    p2.search(s2)
    i += 1
t3 = time.time()

print "\nfail\nmatch:"
print (t1-t0)
print "search:"
print (t3-t2)

The two ways are tested with a string that doesn't match and a string that matches.

results:

succeed
match:
0.000469207763672
search:
0.000494003295898

fail
match:
0.000430107116699
search:
0.46605682373

What can we conclude with these results:

1) The performances are similar when the pattern succeeds

2) The performances are totally different when the pattern fails. This is the most important point because, it means that re.search continues to test each positions of the string even if the pattern is anchored when re.match stops immediatly.

If you increase the size of the failing test string, you will see that re.match doesn't take more time but re.search depends of the string size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM