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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:39:20  来源:igfitidea点击:

linux find regex

regexlinuxbashunixfind

提问by Dijkstra

I'm having trouble using the regex of the findcommand. 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-extendedin 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 -regextypeargument 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 emacstype doesn't support the [[:digit:]]construct. I tried it with posix-extendedand 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 -regexdepends 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 -regexfor what you are doing.

您实际上不必使用-regex您正在做的事情。

find . -iname "*[0-9]"