【发布时间】:2011-08-03 21:17:08
【问题描述】:
我在使用find 命令的正则表达式时遇到问题。可能是我不明白在命令行上转义的东西。
为什么这些不一样?
find -regex '.*[1234567890]'
find -regex '.*[[:digit:]]'
Bash、Ubuntu
【问题讨论】:
-
你有什么输出表明它们不是?
标签: regex linux bash unix find
我在使用find 命令的正则表达式时遇到问题。可能是我不明白在命令行上转义的东西。
为什么这些不一样?
find -regex '.*[1234567890]'
find -regex '.*[[:digit:]]'
Bash、Ubuntu
【问题讨论】:
标签: regex linux bash unix find
你应该看看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:]]'
【讨论】:
find 使用的默认正则表达式语法不支持具有字符类的正则表达式(例如 [[:digit:]])。您需要指定不同的正则表达式类型,例如 posix-extended 才能使用它们。
查看 GNU Find 的正则表达式 documentation,它向您展示了所有正则表达式类型及其支持的内容。
【讨论】:
请注意,-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]"
【讨论】:
好吧,你可以试试这个'.*[0-9]'
【讨论】: