当你不知道引号是单引号还是双引号时,匹配引号是相当容易的:
>>> 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'"]
re.findall('\s+(.+?)=(["\'])(.*?)\2',s)
其中s
是一个类似于stuff name="content" name2='more content'
的字符串。 - Ryan Saxer
才能工作...为什么呢? - Ryan Saxer
?)。它将反斜杠视为反斜杠,因此r'\n' == '\\n'
。如果没有它,你就必须写'\\s+(.+?)=(["\\'])(.*?)\\2
。 - Blender