Java REGEX – Regular Expression String Literals


Development environment

  1. JDK 1.8 Downloads
  2. Eclipse Mars Download

In order to run the example; simply follow the below steps:

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.


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.