Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a faster operation, re.match/search or str.find?

For one off string searches, is it faster to simply use str.find/rfind than to use re.match/search?

That is, for a given string, s, should I use:

if s.find('lookforme') > -1:     do something 

or

if re.match('lookforme',s):     do something else 

?

like image 941
Mike Caron Avatar asked Feb 04 '11 18:02

Mike Caron


People also ask

Is regex faster than find?

Summary: find and in depend on string length and location of pattern in the string while regex is somehow string-length independent and faster for very long strings with the pattern at the end.

What is the difference between re match () and re search ():?

There is a difference between the use of both functions. Both return the first match of a substring found in the string, but re. match() searches only from the beginning of the string and return match object if found.

What is the difference between search () and match () functions?

Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default).

What is the difference between search and find in Python?

findall() helps to get a list of all matching patterns. It searches from start or end of the given string. If we use method findall to search for a pattern in a given string it will return all occurrences of the pattern. While searching a pattern, it is recommended to use re.


1 Answers

The question: which is faster is best answered by using timeit.

from timeit import timeit import re  def find(string, text):     if string.find(text) > -1:         pass  def re_find(string, text):     if re.match(text, string):         pass  def best_find(string, text):     if text in string:        pass  print timeit("find(string, text)", "from __main__ import find; string='lookforme'; text='look'")   print timeit("re_find(string, text)", "from __main__ import re_find; string='lookforme'; text='look'")   print timeit("best_find(string, text)", "from __main__ import best_find; string='lookforme'; text='look'")   

The output is:

0.441393852234 2.12302494049 0.251421928406 

So not only should you use the in operator because it is easier to read, but because it is faster also.

like image 60
user225312 Avatar answered Sep 28 '22 16:09

user225312