正则表达式排除查找工具的单词

regex to exclude words for find tool

我尝试使用 find 实用程序查找文件,我想查找除特殊目录中的文件之外的所有 *.js 文件。我尝试使用命令查找:

find ./ -regex '\.((?\!node_modules).)*\.js'

但是这个场景一无所获。

这个正则表达式适用于我测试过的 Sublime Text 3。

P.S。我知道用下面的其他选项解决问题。但我想知道如何用正则表达式解决问题

find ./ -path *node_modules -prune -o -name *.js

你暂时不能做。

GNU Find 支持多种正则表达式类型,但其中 none 支持零宽度断言。

-regextype type
          Changes  the regular expression syntax understood by -regex and -iregex tests which occur later on the command line.  Currently-implemented types are emacs (this is the default), > posix-awk, posix-basic, posix-
          egrep and posix-extended.

对于您的任务,您需要后向断言, 目前还不支持。

您的另一个选择是使用 find2perl 将查找表达式转换为 perl 程序, 这与表达式的含义相同。

您可以使用此程序检查您的表达式是否适用于 PCRE 和 look-ahead/look-behind 断言。

您的 find 实施的正则表达式引擎似乎不支持环视。 (参见 man find,您可能会找到 @IgorChubin 的解释。)

您仍然可以使用 ! -regex 来排除不需要的模式,例如:

find . ! -regex '\./node_modules/.*' -name '*.js'