regex/linux find命令未按预期工作

regex/linux find命令未按预期工作,regex,bash,find,Regex,Bash,Find,背景:我正试图通过管道将此文件的输出导入xargs wc-l | grep total来获得根web目录中所有代码文件(.html |.htm |.php |.js |.css)的总行数(递归) $ find . -regex '.+\.php' ./inc/db.php ./inc/userauth.php ./index.php .......... etc. $ find . -regex '.+\.js' ./inc/jquery-1.7.1.js ./inc/script.js $

背景:我正试图通过管道将此文件的输出导入
xargs wc-l | grep total
来获得根web目录中所有代码文件(.html |.htm |.php |.js |.css)的总行数(递归)

$ 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)
根据,

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

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

转义字符是特殊的,尽管它确实取决于您的shell(所以我在这里很热心)。

find
使用不同风格的正则表达式,因此您必须编写
\(js\\php\)
,而不仅仅是
(js | php)
,您需要转义bash shell解释的某些字符。你可以在这里找到这些字符的列表:@NickGarvey:我认为这些字符在单引号中使用时没有任何特殊意义。在单引号中使用时没有任何特殊意义。答案中解释的问题是,当不使用ERE时,某些字符需要转义。默认情况下,GNUFind使用emacs正则表达式,这需要转义。啊,你是对的,这是答案中提到的正则表达式语法。我的错误。或者,您可以通过编写
find来指示
find
使用ERE(更像OP心目中的正则表达式)-regextype posix extended-regex'.+\(php | js)
。。。mac需要ERE的选项-E
find . -regex '.+\.\(php\|js\)'