regex for triple quote

后端 未结 5 677
我在风中等你
我在风中等你 2020-12-19 06:32

What regex will find the triple quote comments (possibly multi-line) in a Python source code?

5条回答
  •  臣服心动
    2020-12-19 07:01

    I've found this one from Tim Peters (I think) :

    pat = """
        qqq
        [^\\q]*
        (
        (   \\\\[\000-\377]
            |   q
            (   \\\\[\000-\377]
            |   [^\\q]
            |   q
            (   \\\\[\000-\377]
                |   [^\\q]
            )
            )
        )
        [^\\q]*
        )*
        qqq
    """  
    pat = ''.join(pat.split(), '')  
    tripleQuotePat = pat.replace("q", "'") + "|" + pat.replace('q', '"')  
    

    But, as stated by bobince, regex alone doesn't seem to be the right tool for parsing Python code.
    So I went with tokenize from the standard library.

提交回复
热议问题