Regular Expressions Character classes Non-digits matching (negated character class)


Example

[^0-9]

This will match all characters that are not ASCII digits.

If Unicode digits are also to be negated, the following expression can be used, depending on your flavor/language settings:

[^\d]

This can be shortened to:

\D

You may need to enable Unicode character properties support explicitly by using the u modifier or programmatically in some languages, but this may be non-obvious. To convey the intent explicitly, the following construct can be used (when support is available):

\P{N}

Which by definition means: any character which is not a numeric character in any script. In a negated character range, you may use:

[^\p{N}]

In the following sentences:

  1. Hi, what's up?

  2. I can't wait for 2017!!!

The following characters will be matched:

  1. ,, , ', ?, the end of line character and all letters (lowercase and uppercase).

  2. ', , !, the end of line character and all letters (lowercase and uppercase).