regexphabricator

Regex to catch file name and folder with the same name


I have a folder structure like this:

/repo
   /requirements/ 
      base.txt
      something.txt
   requirements.txt

I need a regex that tracks if a file is within the requirements folder OR the requirements.txt file.

I have this @/requirements/*@ that tracks changes in requirements folder, but how do I find requirements.txt

At the same time I don't want it to catch /repo/apps/myapp/requirements/txt


Solution

  • You can use the regex below for your requirements:

    ^\/repo\/requirements\/?.*?$|^\/repo\/requirements\.txt$
    

    Explanation of the above regex:

    ^, $ - Represents starting and ending delimiter of the test string.

    | - Represents alternation.

    ^\/repo\/requirements\/?.*?$ - This part of regex matches any file inside the requirements directory which in turn is just inside root directory.

    ^\/repo\/requirements\.txt$ - This part of regex matches requirements.txt which is just inside the root directory and not any deeper.

    You can find the demo of the above regex in here.