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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:24:32  来源:igfitidea点击:

Is \d not supported by grep's basic expressions?

linuxgrep

提问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 \dis pcre. You can either pass -Pto 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]\+'