Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected working of Negated Shorthand Character Classes

The Regular expression

/[\D\S]/

should match characters Which is not a digit or not whitespace

But When I test this expression in regexpal

It starts matching any character that's digit, whitespace

What i am doing wrong ?

like image 764
Cody Avatar asked Mar 24 '23 21:03

Cody


1 Answers

\D = all characters except digits, \S = all characters except whitespaces

[\D\S] = union (set theory) of the above character groups = all characters.

Why? Because \D contains \s and \S contains \d.

If you want to match characters which are not dights nor whitespaces you can use [^\d\s].

like image 194
Qtax Avatar answered Apr 13 '23 02:04

Qtax