简体   繁体   English

匹配选项列表中的确切字符串

[英]Match exact string from list of options

I am using pre-commit to run some hooks, and I have a regex where I only include a list of files (dirs) that I wish to run the hook on.我正在使用 pre-commit 来运行一些钩子,并且我有一个正则表达式,其中我只包含我希望在其上运行钩子的文件(目录)列表。 The problem is that my regex matches exact string + any other appending string to the first string.问题是我的正则表达式与第一个字符串完全匹配 + 任何其他附加字符串。

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

here I have initially two apps starting with the string store :在这里,我最初有两个以字符串store开头的应用程序:

store
storespecifics

The above regex matches both, where I am only trying to match exact store .上面的正则表达式匹配两者,我只是想匹配确切的store I tried for example (?=^(analytic|authentication|store)$) , but nothing gets matched.例如,我尝试了(?=^(analytic|authentication|store)$) ,但没有匹配到。

there's no need to involve positive lookaheads here as pre-commit uses re.search to match paths -- this is probably closer to what you want:由于预提交使用re.search来匹配路径,因此无需在此处涉及积极的前瞻——这可能更接近您想要的:

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

note that I use a / here to make sure we're at a folder boundary (matching store/foo.py but not storespecifics/foo.py )请注意,我在这里使用/来确保我们位于文件夹边界(匹配store/foo.py但不匹配storespecifics/foo.py

I also anchor the match to the beginning with ^ , if that's not desirable you'd probably replace the ^ with a /我还使用^将匹配锚定到开头,如果不希望这样做,您可能会将^替换为/


disclaimer: I wrote pre-commit免责声明:我写了预提交

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM