why \s+ do not match the space in macro with grep?

2019-09-15 03:57发布

i want to search the define of macro of ZEND_BEGIN_ARG_INFO_EX ,

so i run the command

 grep   "define\s+ZEND_BEGIN_ARG_INFO_EX" -r  ./ 

but it find nothing .

So,I try

grep   "define\s*ZEND_BEGIN_ARG_INFO_EX" -r  ./

it success return the define of ZEND_BEGIN_ARG_INFO_EX,it return

./Zend/zend_API.h:#define ZEND_BEGIN_ARG_INFO_EX  ....

My problem :

why \s+ do not match the space in macro ?

I think it should match at least one space .

标签: regex shell grep
2条回答
你好瞎i
2楼-- · 2019-09-15 04:26

The standard grep that comes with Linux does not accept +, only *. You could put it like this:

grep "define\s\s*ZEND_BEGIN_ARG_INFO_EX" -r ./ 

or this

grep -E "define\s+ZEND_BEGIN_ARG_INFO_EX" -r ./ 
查看更多
时光不老,我们不散
3楼-- · 2019-09-15 04:50

There are two issues here:

  • \s is PCRE syntax. grep supports BRE (by default) or ERE (when called as egrep or passed the -E argument), but not PCRE absent nonstandard extensions. Use [[:space:]] instead.

  • + is ERE syntax, whereas by default grep uses BRE. Either pass -E to grep, or invoke it as egrep, to enable ERE.

Thus:

grep -Ere "define[[:space:]]+ZEND_BEGIN_ARG_INFO_EX" ./
查看更多
登录 后发表回答