Why use re.match(), when re.search() can do the same thing?
Question: From the documentation, it's very clear that:

  * `match()` -> apply pattern match at the beginning of the string
  * `search()` -> search through the string and return first match

And `search` with `'^'` and without `re.M` flag would work the same as
`match`.

Then why does python have `match()`? Isn't it redundant? Are there any
performance benefits to keeping `match()` in python?


Answer: The `pos` argument behaves differently in important ways:

    
    
    >>> s = "a ab abc abcd"
    >>> re.compile('a').match(s, pos=2)
    <_sre.SRE_Match object; span=(2, 3), match='a'>
    >>> re.compile('^a').search(s, pos=2)
    None
    

`match` makes it possible to write a tokenizer, and ensure that characters are
never skipped. `search` has no way of saying "start from the earliest
allowable character".

Example use of match to break up a string with no gaps:

    
    
    def tokenize(s, patt):
        at = 0
        while at < len(s):
            m = patt.match(s, pos=at)
            if not m:
                raise ValueError("Did not expect character at location {}".format(at))
            at = m.end()
            yield m
    

