Linux grep 的基本表达式不支持 \d 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6901171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is \d not supported by grep's basic expressions?
提问by Ankur Agarwal
This does not generate any output. How come?
这不会产生任何输出。怎么来的?
$ echo 'this 1 2 3' | grep '\d\+'
But these do:
但这些确实:
$ echo 'this 1 2 3' | grep '\s\+'
this 1 2 3
$ echo 'this 1 2 3' | grep '\w\+'
this 1 2 3
采纳答案by Daenyth
grep
's default mode is (iirc) POSIX regex, and \d
is pcre. You can either pass -P
to gnu grep, for perl-like regexps, or use [[:digit:]]
instead of \d
.
grep
的默认模式是 (iirc) POSIX regex,并且\d
是 pcre。您可以通过-P
给的GNU grep的类似Perl的正则表达式,或者使用[[:digit:]]
替代\d
。
daenyth@Bragi ~ $ echo 1 | grep -P '\d'
1
daenyth@Bragi ~ $ echo 1 | grep '[[:digit:]]'
1
回答by Charles Ma
Try this $ echo 'this 1 2 3' | grep '[0-9]\+'
尝试这个 $ echo 'this 1 2 3' | grep '[0-9]\+'