yaml多行正则表达式

问题描述

我想用pygrep一个pre-commit钩子来查找例如

的情况
    .. warning:

(应为.. warning::)。

如果我写

-   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)$

然后这可以工作-但是,字符串太长了。有没有办法将其分成多行?

我尝试过

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

但这是行不通的(结果正则表达式不同)。

有什么建议吗?

解决方法

作为documented,您可以使用冗长表达式:

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

解决方案要做

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