What is the difference between the regular expressions [^\d\s] and [\D\S]

[^\d\s]

will match a single character that is NOT a digit or whitespace.

[\D\S]

will match a single character that IS a non-digit or non-whitespace.

Since every character is either not a digit or not whitespace, the second regex will match any character.

It’s equivalent to the difference between:

if (!(isdigit(c) || isspace(c))) ...

and

if (!isdigit(c) || !isspace(c)) ...

Note that the following would be equivalent to the first one (by deMorgan’s law):

if (!isdigit(c) && !isspace(c)) ...

Leave a Comment

error code: 523