Search code examples
pythonregexyamlpre-commitpre-commit.com

yaml multiline regex


I would like to write a pygrep hook with pre-commit which finds cases of, e.g.

    .. warning:

(when it should be .. warning::).

If I write

-   repo: local
    -   id: incorrect-sphinx-directives
        name: Check for incorrect Sphinx directives
        language: pygrep
        entry: \.\. (autosummary|contents|currentmodule|deprecated|function|image|important|include|ipython|literalinclude|math|module|note|raw|seealso|toctree|versionadded|versionchanged|warning):[^:]
        files: \.(py|pyx|rst)$

then this works - however, the string is unreadably long. Is there a way to split it into multiple lines?

I tried

        entry: "\
            .. (autosummary|contents|currentmodule|deprecated\
            |function|image|important|include|ipython\
            |literalinclude|math|module|note|raw|seealso\
            |toctree|versionadded|versionchanged|warning\
            ):[^:]"

but that doesn't work (the resulting regular expression is different).

Any suggestions?


Solution

  • As documented you can use a verbose expression:

            entry: |
                (?x)^(
                    thing|
                    other_thing|
                    other_other_thing
                )$