Java - Predefined Character Classes

Introduction

Some frequently used predefined character classes are listed in the following Table.

Predefined Character Classes
Meaning
Comments
.
Any character
may or may not match line terminators.
\d
A digit.
Same as [0-9]
\D
A non-digit.
Same as [^0-9]
\s
A whitespace character.
Same as [ \t\n\x0B\f\r]. The list includes a space, a tab, a new line, a vertical tab, a form feed, and a carriage return characters.
\S
A non-whitespace character.
Same as [^\s]
\w
A word character.
Same as [a-zA-Z_0-9]. The list includes lowercase letters, uppercase letter, underscore, and decimal digits.
\W
A non-word character.
Same as [^\w]