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

2019-09-15 04:00发布

问题:

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 .

回答1:

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" ./


回答2:

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 ./ 


标签: regex shell grep