您的位置:首页 > 编程语言 > Python开发

python正则表达式模块re中search和match方法的区别

2017-12-18 10:06 1001 查看
re.
search
(pattern, string, flags=0)
Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding 
MatchObject
 instance.
Return 
None
 if no position in the string matches the pattern; note that this is different from finding a zero-length
match at some point in the string.
re.
match
(pattern, string, flags=0)
If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding 
MatchObject
 instance.
Return 
None
 if the string does not match the pattern; note that this is different from a zero-length match.

For example:

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

https://docs.python.org/2/library/re.html#search-vs-match
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python