【问题标题】:Match exact string from list of options匹配选项列表中的确切字符串
【发布时间】:2022-01-12 17:43:27
【问题描述】:

我正在使用 pre-commit 来运行一些钩子,并且我有一个正则表达式,其中只包含我希望在其上运行钩子的文件(目录)列表。问题是我的正则表达式匹配精确的字符串 + 任何其他附加字符串到第一个字符串。

hooks:
- id: xdoc
  files: (?=(analytic|authentication|store))
  exclude: (?=(apps|serializers|admin|test|migrations|__init__.py))
  stages: [commit]

这里我最初有两个以字符串 store 开头的应用程序:

store
storespecifics

上面的正则表达式匹配两者,我只是想精确匹配store。 例如,我尝试了(?=^(analytic|authentication|store)$),但没有匹配到。

【问题讨论】:

    标签: python regex python-re pre-commit-hook pre-commit.com


    【解决方案1】:

    这里没有必要像 pre-commit uses re.search to match paths 那样涉及积极的前瞻——这可能更接近你想要的:

            files: ^(analytic|authentication|store)/
            exclude: (apps|serializers|admin|test|migrations|__init__.py)
    

    请注意,我在此处使用 / 以确保我们位于文件夹边界(匹配 store/foo.py 但不匹配 storespecifics/foo.py

    我还将匹配以^ 开头,如果不希望这样做,您可能会将^ 替换为/


    免责声明:我写了预提交

    【讨论】:

    • 用 ^ 将匹配锚定到开头也根本不匹配任何结果,但是用 / 替换它。谢谢!
    猜你喜欢
    • 2020-10-23
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多