linux 查找正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5635651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
linux find regex
提问by Dijkstra
I'm having trouble using the regex of the find
command. Probably something I don't understand about escaping on the command line.
我在使用find
命令的正则表达式时遇到问题。可能我不明白在命令行上转义。
Why are these not the same?
为什么这些不一样?
find -regex '.*[1234567890]'
find -regex '.*[[:digit:]]'
Bash, Ubuntu
巴什,Ubuntu
采纳答案by dogbane
Regular expressions with character classes (e.g. [[:digit:]]
) are not supported in the default regular expression syntax used by find
. You need to specify a different regex type such as posix-extended
in order to use them.
使用[[:digit:]]
的默认正则表达式语法不支持具有字符类(例如)的正则表达式find
。您需要指定不同的正则表达式类型posix-extended
才能使用它们。
Take a look at GNU Find's Regular Expression documentationwhich shows you all the regex types and what they support.
查看 GNU Find 的正则表达式文档,它向您展示了所有正则表达式类型及其支持的内容。
回答by bmk
You should have a look on the -regextype
argument of find
, see manpage:
您应该查看 的-regextype
参数find
,请参阅联机帮助页:
-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.
I guess the emacs
type doesn't support the [[:digit:]]
construct. I tried it with posix-extended
and it worked as expected:
我猜该emacs
类型不支持该[[:digit:]]
构造。我试过了posix-extended
,它按预期工作:
find -regextype posix-extended -regex '.*[1234567890]'
find -regextype posix-extended -regex '.*[[:digit:]]'
回答by StKiller
Well, you may try this '.*[0-9]'
嗯,你可以试试这个 '.*[0-9]'
回答by kurumi
Note that -regex
depends on whole path.
请注意,这-regex
取决于整个路径。
-regex pattern
File name matches regular expression pattern.
This is a match on the whole path, not a search.
You don't actually have to use -regex
for what you are doing.
您实际上不必使用-regex
您正在做的事情。
find . -iname "*[0-9]"