Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.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_Python 3.x_Regex_Re - Fatal编程技术网

Python 引用忽略反斜杠的文本正则表达式

Python 引用忽略反斜杠的文本正则表达式,python,python-3.x,regex,re,Python,Python 3.x,Regex,Re,我需要一个正则表达式,该正则表达式将获取引号内的所有文本(“,”) 例如: import re i = 'Test Text 1 "Test Text 2"' re.compile(regex).findall(i) # Returns: ['"Test Text 2', '"'] import re i = 'Test Text 1 "Test Text \\" 2"' re.compile(regex).findall(

我需要一个正则表达式,该正则表达式将获取引号内的所有文本() 例如:

import re
i = 'Test Text 1 "Test Text 2"'
re.compile(regex).findall(i)
# Returns: ['"Test Text 2', '"']
import re
i = 'Test Text 1 "Test Text \\" 2"'
re.compile(regex).findall(i)
# Returns: ['"Test Text " 2', '"']
到现在为止,我已经做了这个正则表达式:

regex = ("(')([^']+)?|(\")([^\"]+)?")
这很好,但有一个问题。我希望它像python引号颜色编码系统一样工作,所以我希望它不会停止元素,如果在结束引号之前有反斜杠。 例如:

import re
i = 'Test Text 1 "Test Text 2"'
re.compile(regex).findall(i)
# Returns: ['"Test Text 2', '"']
import re
i = 'Test Text 1 "Test Text \\" 2"'
re.compile(regex).findall(i)
# Returns: ['"Test Text " 2', '"']
使用

见和


(['”])((?:\\.\124;(?!\ 1)[^\\]*)\1
模式匹配组1中的任何引号,然后匹配任何转义序列(使用
\\.
)或除捕获的引号和反斜杠(
(?!\1)[^\\\]
)以外的任何字符,零次或多次,直到捕获的引号(请参见
\1
).

如果有两个反斜杠,则此连字符正则表达式是否需要在报价前忽略反斜杠?(…实际上是任意偶数个反斜杠。)不。如果它的
“'testtext\\\”
,那么它就不应该忽略它,正如我所说的,就像python引用颜色编码一样。基本上就像你说的,连反斜杠的数量都不应该被忽略。这是可行的,但当没有结束引号时,它会声明后面带有反斜杠的引号作为结束引号。例如:i='测试文本1'测试文本\\“2”