python 单个正则表达式,用于解析Python单引号或双引号字符串,同时保留任何转义的引号字符

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 单个正则表达式,用于解析Python单引号或双引号字符串,同时保留任何转义的引号字符相关的知识,希望对你有一定的参考价值。

"""
Parses single or double-quoted strings while preserving escaped quote chars
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1. captures the first quote character (either a single-quote or double-quote)
   and stores it in a group named "quote"
2. takes zero or more characters up until it matches the character that is
   stored in the group named "quote" (i.e. named referenced group) providing
   that the matching "quote" character is not escaped (preceeded by a backslash
   character, i.e. negative look behind assertion).  All matching characters
   between the quotes are stored in a group named "string".
"""
import re


QUOTED_STRING_RE = re.compile(
    r"(?P<quote>['\"])(?P<string>.*?)(?<!\\)(?P=quote)")


def parse_quoted_string(search_string):
    r"""
    >>> s = '...And the gold of \'the knight\\\'s good banner\' Still waved...'
    >>> parse_quoted_string(s)
    "the knight\\'s good banner"
    >>> s = '"To save my lady!" Fast rode \'the knight\'... by "Stephen Crane"'
    >>> parse_quoted_string(s)
    'To save my lady!'
    >>> print(QUOTED_STRING_RE.findall(s))
    [('"', 'To save my lady!'), ("'", 'the knight'), ('"', 'Stephen Crane')]
    """
    match = QUOTED_STRING_RE.search(search_string)
    if match:
        return match.group('string')

if __name__ == '__main__':
    import doctest
    doctest.testmod()

以上是关于python 单个正则表达式,用于解析Python单引号或双引号字符串,同时保留任何转义的引号字符的主要内容,如果未能解决你的问题,请参考以下文章

python基础之正则表达式

Python爬虫解析神器-正则表达式如何正确运用?案例详解

Python爬虫解析神器-正则表达式如何正确运用?案例详解

用于解析和检查 c++ 的 Python 代码

python正则表达式

Python正则表达式