[英]Match exact string from list of options
我正在使用 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)$)
,但沒有匹配到。
由於預提交使用re.search
來匹配路徑,因此無需在此處涉及積極的前瞻——這可能更接近您想要的:
files: ^(analytic|authentication|store)/
exclude: (apps|serializers|admin|test|migrations|__init__.py)
請注意,我在這里使用/
來確保我們位於文件夾邊界(匹配store/foo.py
但不匹配storespecifics/foo.py
)
我還使用^
將匹配錨定到開頭,如果不希望這樣做,您可能會將^
替換為/
免責聲明:我寫了預提交
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.