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

uqzxnwby  于 2021-06-24  发布在  Hive
关注(0)|答案(2)|浏览(548)

我正在尝试使用regex来匹配包含 .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 不跟在后面 / .

envsm3lx

envsm3lx1#

你可以从中得到一些想法。


**(.*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

rqcrx0a6

rqcrx0a62#

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

^(?:\/[^\/\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

相关问题