我尝试匹配下面的单引号:
s= "name:'abc','hello'"
但似乎match / findall的行为不同:
re.match("\B'\w+'\B", s) # ===> return None
re.findall("\B'\w+'\B", s) #===> RETURN ['abc', 'hello']
实际上这是由字符串中的单引号引起的,任何人都知道发生了什么?
我在win7中使用py2.7.8。
答案 0 :(得分:7)
参见https://docs.python.org/2/library/re.html#search-vs-match - “Python提供了两种基于正则表达式的基本操作:re.match()
仅在字符串的开头检查匹配,而re.search()
检查匹配字符串中的任何位置(这是Perl默认执行的操作)。“
您正在使用re.match()
;如果你切换到re.search()
,你就会得到你期望的行为。