yaml多行正则表达式



我想用pre-commit编写一个pygrep钩子,它可以查找的情况,例如

.. 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
):[^:]"

但这不起作用(生成的正则表达式不同(。

有什么建议吗?

如文档所述,您可以使用verbose表达式:

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
):[^:]"

最新更新