我在使用find
命令的正则表达式时遇到问题。可能有一些我不理解的关于在命令行上进行转义的事情。
为什么这些不一样呢?
find -regex '.*[1234567890]'
find -regex '.*[[:digit:]]'
Bash,Ubuntu
发布于 2011-04-12 21:21:00
find
使用的默认正则表达式语法不支持具有字符类的正则表达式(例如[[:digit:]]
)。您需要指定一个不同的正则表达式类型,比如posix-extended
,才能使用它们。
看看GNU Find的正则表达式documentation,它显示了所有的正则表达式类型及其支持的内容。
发布于 2011-04-12 21:15:42
您应该了解一下find
的-regextype
参数,请参阅manpage
-regextype type
Changes the regular expression syntax understood by -regex and -iregex
tests which occur later on the command line. Currently-implemented
types are emacs (this is the default), posix-awk, posix-basic,
posix-egrep and posix-extended.
我猜emacs
类型不支持[[:digit:]]
构造。我在posix-extended
上试用了一下,结果和预期的一样:
find -regextype posix-extended -regex '.*[1234567890]'
find -regextype posix-extended -regex '.*[[:digit:]]'
发布于 2011-04-12 21:18:51
注意,-regex
依赖于整个路径。
-regex pattern
File name matches regular expression pattern.
This is a match on the whole path, not a search.
你实际上并不需要使用-regex
来做你正在做的事情。
find . -iname "*[0-9]"
https://stackoverflow.com/questions/5635651
复制相似问题