开发者

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

开发者 https://www.devze.com 2023-03-24 15:12 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may b开发者_如何学Goe able to be answered.

Closed 1 year ago.

The community reviewed whether to reopen this question last year and left it closed:

Original close reason(s) were not resolved

Improve this question

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


As specified in POSIX, grep uses basic regular expressions, but \d is part of a Perl-compatible regular expression (PCRE).

If you are using GNU grep, you can use the -P option, to allow use of PCRE regular expressions. Otherwise you can use the POSIX-specified [[:digit:]] character class in place of \d.

echo 1 | grep -P '\d'
# output: 1
echo 1 | grep '[[:digit:]]'
# output: 1


Try this $ echo 'this 1 2 3' | grep '[0-9]\+'

0

精彩评论

暂无评论...
验证码 换一张
取 消