【问题标题】:regex to exclude words for find tool正则表达式排除查找工具的单词
【发布时间】:2016-08-09 13:32:27
【问题描述】:

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

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

但是这个场景什么也没找到。

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

附:我知道如何使用下面的其他选项来解决问题。但我想知道如何用正则表达式解决问题

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

【问题讨论】:

  • 尝试以下操作:find . -name "*.js" | grep -v "<dir1>" | grep -v "dir2" ...(“grep -v”表示“不包含...”)

标签: regex linux bash find regex-lookarounds


【解决方案1】:

你现在做不到。

GNU Find 支持多种正则表达式类型,但它们都不支持零宽度断言。

-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 和前瞻/后瞻断言。

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 谢谢,它可以工作(转义 bash 的感叹号)。 Bui 这不是我问题的确切答案,因为它使用find 的另一个选项(“!”),而不仅仅是正则表达式
    • 你不需要为 Bash 转义 !。顺便说一句,您可以使用另一个-regex 而不是-name 作为find . ! -regex '\./node_modules/.*' -regex '.*\.js',但这不必要地更长,所以我不推荐
    猜你喜欢
    • 2016-05-09
    • 2019-04-30
    • 2013-02-28
    • 2021-04-20
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多