![](/img/trans.png)
[英]regex to match a single quote string that could contain ''' '' '''
[英]python regex match single quote
我尝试匹配下面的单引号:
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。
请参阅https://docs.python.org/2/library/re.html#search-vs-match - “Python基于正则表达式提供两种不同的基本操作: re.match()
仅检查匹配时的匹配字符串的开头,而re.search()
检查字符串中的任何位置(这是Perl默认执行的操作)。“
你正在使用re.match()
; 如果切换到re.search()
,您将获得您期望的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.