如何在查找中使用正则表达式



我试图使用正则表达式查找。下面是我的命令:

for GLOBALS_DEFINITIONS in `find . -type f -name -regex 'globalSettings_d+.json'`; do

在结果:

find: paths must precede expression:

-name参数导致的错误信息。您应该使用-name-regex,但不能同时使用:

find . -type f -regex 'globalSettings_d+.json'
但是,请注意正则表达式必须匹配完整路径,而不仅仅是文件名。那么,假设你只想匹配当前目录下的文件,你可以这样做:
find . -type f -regex './globalSettings_d+.json'

如果更深的匹配是可以的(这通常是首先使用find的原因),您可以这样做:

find . -type f -regex '.*/globalSettings_d+.json'

但是,关于d部分,请注意默认情况下,find使用Emacs的regexp语法,其中d不匹配数字。你可以这样做:

find . -type f -regex '.*/globalSettings_[0-9]+.json'

最新更新