【问题标题】:regex/linux find command not working as expectedregex/linux find 命令没有按预期工作
【发布时间】:2012-03-14 07:55:37
【问题描述】:

背景:我正在尝试通过管道输出这个变成xargs wc -l | grep total

$ find . -regex '.+\.php'
./inc/db.php
./inc/userauth.php
./index.php
.......... etc.

$ find . -regex '.+\.js'
./inc/jquery-1.7.1.js
./inc/script.js

$ find . -regex '.+\.(php|js)'
(returns nothing)

根据this

abc(def|xyz) matches abcdef or abcxyz

那么.+\.(php|js)不应该匹配所有的.php文件和.js文件吗?

【问题讨论】:

  • 您需要转义某些由 bash shell 解释的字符。您可以在此处找到这些字符的列表:grymoire.com/Unix/Quote.html
  • @NickGarvey:我认为这些字符在单引号中使用时不会有任何特殊含义。
  • 单引号中没有任何特殊含义。答案中解释的问题是,在不使用 ERE 时,某些字符需要转义。默认情况下,GNU find 使用 emacs 正则表达式,这需要转义。
  • 啊,你是对的,它是答案中提到的正则表达式语法。我的错。

标签: regex bash find


【解决方案1】:
find . -regex '.+\.\(php\|js\)'

特殊的转义字符,虽然它确实取决于你的外壳(所以我在这里很热心)。

【讨论】:

    【解决方案2】:

    find 使用不同风格的正则表达式,所以你必须写 \(js\|php\) 而不仅仅是 (js|php)

    【讨论】:

    • +1。或者,您可以通过编写 find . -regextype posix-extended -regex '.+\.(php|js)' 来指示 find 使用 ERE(更像是 OP 考虑的正则表达式)。
    • .. 并且 mac 需要 ERE 的选项 -E
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 2018-09-16
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多