Python - re.match vs. re.search

Possible duplicate:
What is the difference between Pythons re.search and re.match?

I recently moved on to understanding regex with python.

I looked at the api; I can not understand the difference between:

re.match vs. re.search

when is it best to use each of them? pros? minuses?

Please thanks.

+42
python regex
Jul 27 '10 at 17:11
source share
3 answers

re.match() matches only the beginning of a line. Conventional prey. See the documentation.

+47
Jul 27 '10 at 17:13
source share

From search() vs. match() :

re.match() checks for compliance only at the beginning of a line, and re.search() checks for compliance anywhere in the line.

 >>> re.match("c", "abcdef") # No match >>> re.search("c", "abcdef") # Match <_sre.SRE_Match object at ...> 
+35
Jul 27 '10 at 17:14
source share

I just found out that you can also look for substrings like this:

 if 'c' in 'abcdef' # True 
+7
Jul 28 '10 at 19:04
source share



All Articles