正则表达式捕获具有相同名称的文件名和文件夹

Regex to catch file name and folder with the same name

我有这样的文件夹结构:

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

我需要一个正则表达式来跟踪文件是否在需求文件夹或 requirements.txt 文件中。

我有这个 @/requirements/*@ 可以跟踪需求文件夹中的更改,但我如何找到 requirements.txt

同时不想让它被抓到/repo/apps/myapp/requirements/txt

您可以使用下面的正则表达式来满足您的要求:

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

上面正则表达式的解释:

^, $ - 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.

你可以在here.

中找到上述正则表达式的演示