dugres
dugres

Reputation: 13113

regex for triple quote

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

Upvotes: 11

Views: 4706

Answers (5)

Alan Cabrera
Alan Cabrera

Reputation: 732

I don't know how well this will fair when scanning Python code, but this seems to match Python strings in isolation.

^(\"([^\"\n\\]|\\[abfnrtv?\"'\\0-7]|\\x[0-9a-fA-F])*\"|'([^'\n\\]|\\[abfnrtv?\"'\\0-7]|\\x[0-9a-fA-F])*'|\"\"\"((?!\"\"\")[^\\]|\\[abfnrtv?\"'\\0-7]|\\x[0-9a-fA-F])*\"\"\")$

The escaping is not standard Python; this is something that I cut-n-pasted from a project. See it in action at regex101.com.

Upvotes: 0

ak2600
ak2600

Reputation: 5897

I find this to be working perfectly for me (used it with TextMate):

"{3}([\s\S]*?"{3})

I wanted to remove all comments from a library and this took care of the triple-quote comments (single or multi-line, regardless of where they started on the line).

For hash comments (much easier), this works:

#.*$

I used these with TextMate, which uses the Oniguruma regular expression library by K. Kosako (http://manual.macromates.com/en/regular_expressions)

Upvotes: 4

dugres
dugres

Reputation: 13113

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.

Upvotes: 0

bobince
bobince

Reputation: 536795

Python is not a regular language and cannot reliably be parsed using regex.

If you want a proper Python parser, look at the ast module. You may be looking for get_docstring.

Upvotes: 11

SilentGhost
SilentGhost

Reputation: 320049

re.findall('(?:\n[\t ]*)\"{3}(.*?)\"{3}', s, re.M | re.S)

captures only text within triple quotes that are at the begging of a line and could be preceded by spaces, tabs or nothing, as python docstrings should be.

Upvotes: 5

Related Questions