O'Reilly logo

Java 9 Regular Expressions by Anubhava Srivastava

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Predefined shorthand character classes

As we have seen from the preceding examples, certain character classes, such as digits [0-9] or word characters [0-9A-Za-z_], are used in most regex patterns. The Java language, like all regular expression flavors, provides convenient predefined character classes for these character classes. Here is the list:

Shorthand Class Meaning Character Class
\d A digit 0-9 [0-9]
\D A non-digit [^\d]
\w A word character [a-zA-Z0-9_]
W A non-word character [^\w]
\s A whitespace character, including line break [ \t\r\n\f\x0B]
\S A non-whitespace chacracter [^\s]
\h A horizontal whitespace character [ \t\xA0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]
\H A non-horizontal whitespace character [^\h] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required