可能重复:
What is the difference between Python’s re.search and re.match?蟒蛇 - re.match对re.search
我最近一直在跳进理解正则表达式与蟒蛇。
我一直在看api;我似乎无法理解的区别:对re.search
re.match的时候是最好使用每一种?利弊?利弊?
请谢谢。
可能重复:
What is the difference between Python’s re.search and re.match?蟒蛇 - re.match对re.search
我最近一直在跳进理解正则表达式与蟒蛇。
我一直在看api;我似乎无法理解的区别:对re.search
re.match的时候是最好使用每一种?利弊?利弊?
请谢谢。
re.match()
仅匹配字符串的开头。一个共同的问题。请参阅documentation。
re.match()
检查只在字符串的开头匹配,而re.search()
检查匹配字符串中的任何地方。
>>> re.match("c", "abcdef") # No match
>>> re.search("c", "abcdef") # Match
<_sre.SRE_Match object at ...>
我刚刚得知,你还可以搜索像这样子:
if 'c' in 'abcdef'
# True
好主意,如果你不需要它们,就避免使用正则表达式。 “有些人在遇到问题时想'我知道,我会用正则表达式'。”现在他们有两个问题。“杰米Zawinski – neuronet 2017-08-19 19:34:16
步骤1.搜索。 http://stackoverflow.com/questions/180986/what-is-the-difference-between-pythons-re-search-and-re-match。搜索完毕后,请提出**特定**问题。 – 2010-07-27 18:51:36