Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将所有完整引号与正则表达式匹配_Python_Regex - Fatal编程技术网

Python 将所有完整引号与正则表达式匹配

Python 将所有完整引号与正则表达式匹配,python,regex,Python,Regex,因此,当您不知道是单引号还是双引号时,匹配引号是相当容易的: >>> s ="""this is a "test" that I am "testing" today""" >>> re.findall('[\'"].*?[\'"]',s) ['"test"', '"testing"'] 这将在字符串中搜索单引号或双引号,并获取介于两者之间的内容。但问题是: 如果字符串包含其他类型的引号,它将关闭字符串!这里有两个例子来说明我的意思: >>>

因此,当您不知道是单引号还是双引号时,匹配引号是相当容易的:

>>> 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'"]

将第一个字符类包装在捕获组中,然后在另一侧用
\1
引用它:

>>> re.findall(r'([\'"])(.*?)\1',s)
[('"', 'test'), ('"', "won't work right")]

伟大的然后我可以使用列表理解来让正确的列表等待,在我的实际情况下,它返回一个空白列表。有什么问题吗…
re.findall('\s+(.+?)=([“\'])(.*?\2',s)
其中s是一个看起来像
stuff name=“content”的字符串“name2='more content'
等等。没关系……它只对前面的
r
起作用……这是为什么?@RyanSaxe:这可能不是XML/HTML,是吗?至于您的问题,您必须使用原始字符串(注意字符串文本开头之前的小
r
)。它将反斜杠视为反斜杠,因此
r'\n'=='\\n'
。如果没有它,您必须编写
'\\s+(.+?)=([“\\'])(.*)\\2