1

While reading the docs, I found out that the whole difference between re.match() and re.search() is that re.match() starts checking only from the beginning of the string.

>>> import re
>>> a = 'abcde'
>>> re.match(r'b', a)
>>> re.search(r'b', a)
<_sre.SRE_Match object at 0xffe25c98>
>>> re.search(r'^b', a)
>>>

Is there anything I am misunderstanding, or is there no difference at all between re.search('^' + pattern) and re.match(pattern)?

Is it a good practice to only use re.search()?

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
AgvaniaRekuva
  • 565
  • 1
  • 5
  • 12

2 Answers2

5

You should take a look at Python's re.search() vs. re.match() document which clearly mentions about the other difference which is:

Note however that in MULTILINE mode match() only matches at the beginning of the string, whereas using search() with a regular expression beginning with '^' will match at the beginning of each line.

>>> re.match('X', 'A\nB\nX', re.MULTILINE) # No match

>>> re.search('^X', 'A\nB\nX', re.MULTILINE) # Match

<_sre.SRE_Match object; span=(4, 5), match='X'>

The first difference (for future readers) being:

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

For example:

>>> re.match("c", "abcdef") # No match

>>> re.search("c", "abcdef") # Match

<_sre.SRE_Match object; span=(2, 3), match='c'>

Regular expressions beginning with '^' can be used with search() to restrict the match at the beginning of the string:

>>> re.match("c", "abcdef") # No match

>>> re.search("^c", "abcdef") # No match

>>> re.search("^a", "abcdef") # Match

<_sre.SRE_Match object; span=(0, 1), match='a'>

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
2

If you look at this from a code golfing perspective, I'd say there is some use in keeping the two functions separate.

If you're looking from the beginning of the string, re.match, would be preferable to re.search, because the former has one character less in its name, thus saving a byte. Furthermore, with re.search, you also have to add the start-of-line anchor ^ to signify matching from the start. You don't need to specify this with re.match because it is implied, further saving another byte.

cs95
  • 379,657
  • 97
  • 704
  • 746