所以当你不知道它是单引号还是双引号时匹配引号是相当容易的:
>>> s ="""this is a "test" that I am "testing" today"""
>>> re.findall('[\'"].*?[\'"]',s)
['"test"', '"testing"']
这将在字符串中搜索单引号或双引号并获取其间的内容。但这里有一个问题:
如果字符串包含其他类型的引号,它将关闭字符串!这里有两个例子来说明我的意思:
>>> s ="""this is a "test" and this "won't work right" at all"""
>>> re.findall('[\'"].*?[\'"]',s)
['"test"', '"won\'']
>>> s ="""something is "test" and this is "an 'inner' string" too"""
>>> re.findall('[\'"].*?[\'"]',s)
['"test"', '"an \'', '\' string"']
正则表达式'[\'"].*?[\'"]'
将匹配单引号和双引号,这显然很糟糕。
那么什么正则表达式将匹配两种类型的引号,但仅匹配以相同类型的引号结尾的实际字符串。
万一你感到困惑
这是我想要的输出:
s ="""this is a "test" and this "won't work right" at all"""
re.findall(expression,s)
#prints ['"test"','"won\'t work right"']
s ="""something is "test" and this is "an 'inner' string" too"""
re.findall(expression,s)
['"test"', '"an \'inner\' string"',"'inner'"]