7.2.5.3. search() vs. match()
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:
Regular expressions beginning with '^'
can be used with search()
to restrict the match at the beginning of the string:
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.
>>> print(re.match(r"\d+", "123fasdf1234").group(0))
123
>>> print(re.match(r"\d+", "123fasdf1234").start())
0
>>> print(re.match(r"\d+", "123fasdf1234").end())
3