【问题标题】:yaml multiline regexyaml 多行正则表达式
【发布时间】:2021-02-03 07:41:40
【问题描述】:

我想用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\
            ):[^:]"

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

有什么建议吗?

【问题讨论】:

  • 我没有使用正则表达式来修改yaml,不知道你的意思

标签: python regex yaml pre-commit pre-commit.com


【解决方案1】:

作为documented,您可以使用详细表达式:

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

【讨论】:

  • 可能值得一提的是|really does。使用> 也是可行的。
  • @DaSourcerer 对于忽略外部空格的 python 详细正则表达式没有区别
  • 是的。恕我直言,它仍然是 YAML 的一个有用的特性。
  • 当然可以,但是这个问答没关系
【解决方案2】:

解决办法

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 2011-04-13
    • 2022-12-07
    • 1970-01-01
    相关资源
    最近更新 更多