regex/linux find命令无法按预期工作

And*_*sen 4 regex bash find

背景:我试图通过将其输出汇总到我的根web目录(递归)中获取所有代码文件(.html | .htm | .php | .js | .css)的总行数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)
Run Code Online (Sandbox Code Playgroud)

根据,

abc(def|xyz) matches abcdef or abcxyz
Run Code Online (Sandbox Code Playgroud)

所以不应该.+\.(php|js)匹配所有.php文件和.js文件?

Tik*_*vis 6

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

  • +1.或者,您可以通过编写`find来指示`find`使用ERE(更像是OP所考虑的正则表达式).-regextype posix-extended -regex'.+ \.(php | js)'`. (3认同)