linux 查找正则表达式

发布于 2024-10-31 18:55:58 字数 183 浏览 3 评论 0原文

我在使用 find 命令的正则表达式时遇到问题。可能我不明白在命令行上转义的事情。

为什么这些不一样?

find -regex '.*[1234567890]'
find -regex '.*[[:digit:]]'

bash、Ubuntu

I'm having trouble using the regex of the find command. Probably something I don't understand about escaping on the command line.

Why are these not the same?

find -regex '.*[1234567890]'
find -regex '.*[[:digit:]]'

Bash, Ubuntu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紙鸢 2024-11-07 18:55:58

您应该查看 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-extend 尝试了它,它按预期工作:

find -regextype posix-extended -regex '.*[1234567890]'
find -regextype posix-extended -regex '.*[[:digit:]]'

You should have a look on the -regextype argument of find, see 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. 

I guess the emacs type doesn't support the [[:digit:]] construct. I tried it with posix-extended and it worked as expected:

find -regextype posix-extended -regex '.*[1234567890]'
find -regextype posix-extended -regex '.*[[:digit:]]'
許願樹丅啲祈禱 2024-11-07 18:55:58

find 使用的默认正则表达式语法不支持具有字符类的正则表达式(例如 [[:digit:]])。您需要指定不同的正则表达式类型,例如 posix-extended 才能使用它们。

查看 GNU Find 的正则表达式文档它显示了所有正则表达式类型及其支持的内容。

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.

Take a look at GNU Find's Regular Expression documentation which shows you all the regex types and what they support.

你怎么敢 2024-11-07 18:55:58

请注意,-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]"

Note that -regex depends on whole path.

 -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.

find . -iname "*[0-9]"
这个俗人 2024-11-07 18:55:58

好吧,你可以试试这个'.*[0-9]'

Well, you may try this '.*[0-9]'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文