Grep with regexp: spaces don't match unless I add assertions

GNU grep 2.5.4 on bash 4.1.5 (1) on Ubuntu 10.04

It corresponds

$ echo "this is a line" | grep 'a[[:space:]]\+line' this is a line 

But it is not

 $ echo "this is a line" | grep 'a\s\+line' 

But this also matches

 $ echo "this is a line" | grep 'a\s\+\bline' this is a line 

I do not understand why # 2 does not match (while # 1), and # 3 also shows a match. What is the difference here?

+6
source share
2 answers

Take a look at the grep man page. Perl added many regular expression extensions that were not in the original specification. However, since they have proven so useful, many programs have adopted them.

Unfortunately, grep sometimes gets stuck in the past because you want your grep remain compatible with older grep versions.

Some systems have egrep with some extensions. Others allow you to use grep -E to get them. The rest have grep -P , which allows you to use Perl extensions. I believe that the grep command for Linux systems can use the -P extension, which is not available on most Unix systems, unless someone has replaced grep with the GNU version. Newer versions of Mac OS X also support the -P switch, but not older versions.

+4
source

grep does not support the full set of regular expressions, so try using -P to include perl regular expressions. You do not need to avoid + ie

 echo "this is a line" | grep -P 'a\s+line' 
+1
source

Source: https://habr.com/ru/post/894729/


All Articles