正则表达式与Linux找不到匹配项

时间:2020-09-14 20:25:00

标签: regex find

我很茫然,使这个简单的命令不返回任何匹配项。在下面的所有变体中,我得到零结果:

find . -type d -regex '.+ [0-9]{4}' -print | wc -c;
find * -type d -regex '.+[0-9]{4}' -print | wc -c;
find * -type d -regex '.+[0-9]' -print | wc -c;
find . -type d -regex '[0-9]' -print | wc -c;

如果我使用'。+'正则表达式,则将得到所有预期的结果。目录是以这种模式命名的:[1个或多个单词] [4位数字]。我想匹配任何以一年结尾的目录并进行打印。我在这里想念什么?

2 个答案:

答案 0 :(得分:1)

您需要对扩展的正则表达式类型使用-regextype选项,例如:

find . -type d -regextype egrep -regex '.+-[0-9]{4}$

请注意,-regextype的默认值为emacs

答案 1 :(得分:1)

在您的正则表达式中使用-regextype 'posix-extended'

find . -type d -regextype 'posix-extended' -regex '.*[0-9]{4}' -print | wc -c

此表达式将返回所有以四位数结尾的目录。

相关问题