python正则表达匹配单引号

Gin*_*Luo 2 python regex

我尝试匹配下面的单引号:

s= "name:'abc','hello'"
Run Code Online (Sandbox Code Playgroud)

但似乎match/findall的行为是不同的:

re.match("\B'\w+'\B", s)   # ===> return None

re.findall("\B'\w+'\B", s)  #===> RETURN ['abc', 'hello']
Run Code Online (Sandbox Code Playgroud)

实际上这是由字符串中的单引号引起的,任何人都知道发生了什么?

我在win7中使用py2.7.8.

rmu*_*unn 7

请参阅https://docs.python.org/2/library/re.html#search-vs-match - "Python基于正则表达式提供两种不同的基本操作:re.match()仅在字符串的开头检查匹配,同时re.search()检查字符串中任何位置的匹配(这是Perl默认执行的操作)."

你正在使用re.match(); 如果你切换到re.search(),你会得到你期望的行为.