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'>

将上面代码敲一遍,立马就明白它们之间的差别。

posted @ 2019-03-10 10:16  agichen  阅读(307)  评论(0编辑  收藏  举报