一尘不染

REGEX-字符串和转义引号

python

如何获得以下两个文本中引号之间的含义?

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

阅读 253

收藏
2021-01-20

共1个答案

一尘不染

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

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

说明:

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