首页 > 系统服务 > 详细

linux 学习过程中的坑之 find 正则表达式

时间:2019-05-15 10:58:35      阅读:114      评论:0      收藏:0      [点我收藏+]
1 标准的正则表示式 格式
. 表示任意单个字符
* 表示任意次数
+ 表示1次或1次以上
{3} 表示精确匹配次数为3次
{n,m}表示n次到m 次之间
^ 行首锚定 $行尾锚定
\< 单词首部锚定 \> 单词尾部锚定
2 扩展的正则表达式 相对标准的正则表达式 在次数表示的方面只是少了\ 其他都一样
那么问题来了
find -regex 此时的regex 是使用的正则表达式
技术分享图片
此时用到+ 没有使用\+ 表示他是使用的扩展的正则表达式,
在实验环境中 所有的文件为 issue issue1 s1 scripts/ ssss
find -regex ".*s{1,}" 的命令既然无法匹配 到ssss 此文件
find -regex ".*s+" 命令却可以匹配

 迷惑中搜度娘 看看有没有帮助 发现一文章

https://unix.stackexchange.com/questions/67192/find-command-with-regex-quantifier-e-g-1-2

 我们的问题是一致的,那么这个 -regex的选项 到底是不是我们平时使用的正则表达式了?
 man 帮助文档
 -regex pattern
          File  name matches regular expression pattern.  This is a match on the whole path, not a search.  For example, to match a file named `./fubar3‘, you can
          use the regular expression `.*bar.‘ or `.*b.*3‘, but not `f.*r3‘.  The regular expressions understood by find are by default Emacs Regular  Expressions,
          but this can be changed with the -regextype option.

        发现没有说明 只是说有个 -regextype 选项有介绍他采用的模式
         -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 风格的 非我们常用的 posix-egrep and posix-extended 格式的正则表达式。问题解决 ,
总结 新手在学习过程中第一时间肯定是怀疑自己的命令输入有问题,根本就不会考虑使用不同风格的问题,等自己确定自己的命令输入没有问题了,才会开始查帮助文档。总的来说 官方的文档其实还是挺全的,就是英语不好的同学,查看起来特别花时间。

linux 学习过程中的坑之 find 正则表达式

原文:https://blog.51cto.com/2858259639/2394148

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!