Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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. 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
storespecifics

The above regex matches both, where I am only trying to match exact store.
I tried for example (?=^(analytic|authentication|store)$), but nothing gets matched.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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:

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)

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading