python - REGEX 字符串和转义引号

标签 python regex

<分区>

如何获取下面两段文字引号之间的内容?

text_1 = r""" "Some text on \"two\" lines with a backslash escaped\\" \
     + "Another text on \"three\" lines" """

text_2 = r""" "Some text on \"two\" lines with a backslash escaped\\" + "Another text on \"three\" lines" """

对我来说问题是引号如果被转义应该被忽略,但是反斜杠有可能被转义。

我想获取以下组。

[
    r'Some text on \"two\" lines with a backslash escaped\\',
    r'Another text on \"three\" lines'
]

最佳答案

"(?:\\.|[^"\\])*"

匹配带引号的字符串,包括其中出现的任何转义字符。

解释:

"       # Match a quote.
(?:     # Either match...
 \\.    # an escaped character
|       # or
 [^"\\] # any character except quote or backslash.
)*      # Repeat any number of times.
"       # Match another quote.

关于python - REGEX 字符串和转义引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16130404/

相关文章:

regex - VBA正则表达式问题

python - Graphviz:为具有彩虹效果的线条着色

python - 如何从另一个框架控制一个框架?

python - 如何使 gtk 中的两个元素具有相同的大小?

java - 第一个字符为字母且在 Java 中具有特定范围的用户名的正则表达式是什么?

python - 查找字符串的哪一部分与正则表达式python不匹配

python - 有效地计算转换矩阵 (m*m) * (n*n) 的逐元素乘积以给出 (mn*mn) 矩阵

python - 用python替换odb中的状态变量名称

java - 正则表达式函数帮助

regex - 用于计算字符串长度是否大于 16 且小于 13