Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why [^\d\w\s,] matches "leonardo,davinci"?

Tags:

regex

grep

I can't understand why the regexp:

[^\d\s\w,]

Matches the string:

"leonardo,davinci"

That is my test:

$ echo "leonardo,davinci" | egrep '[^\d\w\s,]'
leonardo,davinci

While this works as expected:

$ echo "leonardo,davinci" | egrep '[\S\W\D]'
$ 

Thanks very much

like image 928
Luca Avatar asked Jan 08 '23 16:01

Luca


1 Answers

It's because egrep doesn't have the predefined sets \d, \w, \s. Therefore, putting slash in front of them is just matching them literally:

leonardo,davinci

echo "leonardo,davinci" | egrep '[^a-zA-Z0-9 ,]'

Will indeed, not match.


If you have it installed, you can use pcregrep instead:
echo "leonardo,davinci" | pcregrep '[^\w\s,]'
like image 138
ndnenkov Avatar answered Jan 25 '23 04:01

ndnenkov