[小知识] 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” 又属于哪类正则表达式呢?

请参考这里:http://www.zytrax.com/tech/web/regex.htm

猜你在找的正则表达式相关文章