Java REGEX – Regular Expression Predefined Character Classes

Prerequisites

Development environment

  1. JDK 1.8 Downloads
  2. Eclipse Mars Download

The Pattern API contains a number of useful predefined character classes, which offer convenient shorthands for commonly used regular expressions:

Construct Description
. Any character (may or may not match line terminators)
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]

Step 1 Create Java Project

Launch Eclipse IDE; Create new Java Project by going to File -> New -> Others… -> Java Project. Choose any project name you want then click Finish.

Step 2 Create Java Class

Copy the code below to clipboard; Select src folder in your project; Press CTRL + V; Eclipse IDE will automatically create package & class file with the code that’s pasted from clipboard.

Step 3 Run The Application

Right click to the class; select Run As -> Java Application.

Output

The match still succeeds, even though the dot “.” is not present in the input string. It succeeds because the dot is a metacharacter — a character with special meaning interpreted by the matcher. The metacharacter “.” means any character which is why the match succeeds in this example.

The metacharacters supported by this API are: <([{\^-=$!|]})?*+.>

Note: In certain situations the special characters listed above will not be treated as metacharacters. You’ll encounter this as you learn more about how regular expressions are constructed. You can, however, use this list to check whether or not a specific character will ever be considered a metacharacter. For example, the characters @ and # never carry a special meaning.
There are two ways to force a metacharacter to be treated as an ordinary character:

  • Precede the metacharacter with a backslash, or
  • Enclose it within \Q (which starts the quote) and \E (which ends it).

When using this technique, the \Q and \E can be placed at any location within the expression, provided that the \Q comes first.