1. 程式人生 > >[小知識] grep中如何使用正則表示式匹配數字

[小知識] grep中如何使用正則表示式匹配數字

如何使用grep搜尋包含數字的行?

記得“\d”可以表示任意數字,遂寫成grep "\d" test.txt 

哭沒有返回任何資料, 難道不支援“\d”疑問

你在命令列下執行man grep 檢視幫助,可以看到grep是支援多種正則表示式的,需要用引數去切換。

Matcher Selection
       -E, --extended-regexp
              Interpret PATTERN as an extended regular expression (ERE, see below).  (-E is specified by POSIX.)

       -F, --fixed-strings
              Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.  (-F is specified by POSIX.)

       -G, --basic-regexp
              Interpret PATTERN as a basic regular expression (BRE, see below).  This is the default.

       -P, --perl-regexp
              Interpret  PATTERN as a Perl regular expression (PCRE, see below).  This is highly experimental and grep -P may warn of unimplemented
              features.

而預設情況下,grep只支援basic regex

“\d” 不被支援是因為它不屬於basic regex。

那麼basic regex有哪些?“\d” 又屬於哪類正則表示式呢?