linux 學習過程中的坑之 find 正則表達式

1 標準的正則表示式 格式
. 表示任意單個字符
* 表示任意次數
+ 表示1次或1次以上
{3} 表示精確匹配次數爲3次
{n,m}表示n次到m 次之間
^ 行首錨定 $行尾錨定
\< 單詞首部錨定 \> 單詞尾部錨定
2 擴展的正則表達式 相對標準的正則表達式 在次數表示的方面只是少了\ 其他都一樣
那麼問題來了
find -regex 此時的regex 是使用的正則表達式
linux 學習過程中的坑之 find 正則表達式
此時用到+ 沒有使用\+ 表示他是使用的擴展的正則表達式,
在實驗環境中 所有的文件爲 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 格式的正則表達式。問題解決 ,
總結 新手在學習過程中第一時間肯定是懷疑自己的命令輸入有問題,根本就不會考慮使用不同風格的問題,等自己確定自己的命令輸入沒有問題了,纔會開始查幫助文檔。總的來說 官方的文檔其實還是挺全的,就是英語不好的同學,查看起來特別花時間。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章