正则表达式匹配某些子文件夹并忽略其子文件夹

Regex matching certain subfolder and ignore its subfolder

我正在尝试使用正则表达式来匹配所有包含 .Trash 文件的文件夹,但是当我们找到带有 .Trash 的某个层子文件夹时,我不想匹配。

我正在使用 /[^/]+/*.Trash,但它也匹配 .Trash 的子文件夹。
如何忽略子文件夹?

例如:

/a/b/.Trash+other words -> good
/a/b/.Trash  -> good
/a/b/.blahTrash -> good
/a/.Trash -> good
/a/b/.Trash/c/d -> bad, because we already found /a/b/.Trash

因为.Trash后面还可以跟其他字符或者数字,所以我不能用.Trash后面不跟/

您可以使用以下正则表达式:

^(?:\/[^\/\s]+)+Trash[^\/]*$

演示:https://regex101.com/r/lRp4lF/3/

/a/b/.Trash -> good
/a/b/.Trash+other words -> good
/a/b/.Trash  -> good
/a/b/.blahTrash -> good
/a/.Trash -> good
/a/b/.Trash/c/d -> bad

你可能会从中得到一些启发。

**(.*trash[^\/]+)**

Match 1
1.  /a/b/.Trash+other words -> good
Match 2
1.  /a/b/.Trash -> good
Match 3
1.  /a/b/.blahTrash -> good
Match 4
1.  /a/.Trash -> good

https://rubular.com/r/kGvbSIkHFfT9dM