python re之search/match差别
search → find something anywhere in the string and return a match object.
match → find something at the beginning of the string and return a match object.
代码演示差别:
In [1]: import re
In [2]: string_with_newlines = """something someotherthing"""
In [3]: re.match('some', string_with_newlines)
Out[3]: <re.Match object; span=(0, 4), match='some'>
In [4]: re.match('someother', string_with_newlines)
In [5]: re.search('someother', string_with_newlines)
Out[5]: <re.Match object; span=(10, 19), match='someother'>
将上面代码敲一遍,立马就明白它们之间的差别。