ThatWasFast – Java Regular Expressions (Regex) Tutorial

So, today we are going to understand the usage of Java Regular Expressions. First we will see what are regular expressions in general and then classes required to use the same in Java.

What is Regular Expression?

Regular expression is a string made of special combinations of characters. This is not an ordinary string actually, it is a special string which defines a pattern.

A pattern can be expressed in the form of regular expression. Multiple strings can match to the same regular expression. These strings are said to follow the same pattern.

For example, [email protected], [email protected], [email protected] are all email addresses. But if you notice carefully, they all follow the same pattern. This pattern is

some string (followed by a “@”) domain name (followed by a “.”) top-level domain.

Regular expression (“regex” in short) is a language to specify a single pattern that matches all such strings. So that we can use a single pattern to check for any email.

Please note: We are not covering how to create regular expressions. If you wish to learn the same, head over to Regular Expressions Cheat Sheet

Applications of Regular Expressions

Various fields in Computer Science apply Regular Expressions in their own way.

Some of the most used applications of regex rare:

  • String search algorithms
  • Find-and-replace boxes
  • E-mail address validations (on user input)


So, to check if an e-mail address is valid, you will match an input string against a pattern regex. If the result is true, it means input string can be specified using the pattern regex (and email is valid). And email is invalid, if result is false.


How to Use Regular Expressions in Java


Like Math class functionalities are located in java.util package, regex classes of Java are located in  java.util.regex package. This API is present since Java 1.4.

The below image describes the use of important classes in this package:

Pattern Matching Using Java Regular Expressions API

Classes for java.util.regex API

The below image describes how the objects in this package can be used:

Usage of Regex API for Java Regular Expressions

Usage of Regex API in Java

As you can see, these are very easy to use objects.

Examples to use Java Regular Expressions


There are two ways you will be using regular expressions:

  1. To match one input string against one pattern
  2. To match multiple input strings against one pattern

Java regex code to match one input string against one pattern


Type 1 is slightly easier than type 2. Here is the code for it:

Here, we have not really used Pattern and Matcher objects. A simple Pattern.matches ( ) static method does all the work and returns a Boolean true / false indicating the result of the match.

This method works well – but only if you have to execute pattern match only once. There are disadvantages if this method is used for matching multiple strings to the same pattern:

  • Pattern will be compiled repeatedly, which is inefficient code.
  • Performs only case sensitive search.

Java regex code to match multiple input strings against one pattern


Type 2 can be used if the same pattern is to be used multiple times to match different strings. Here is the code for it:

Here, observe three main things in this code sample:

  • Pattern class does not have a constructor. So we use Pattern.compile ( ) static method to create the pattern object. This object holds the compiled and reusable version of the pattern.
  • We use Patterm.matcher ( ) static method and supply our input text to this method. This method returns an instance of the Matcher class.
  • Finally, we call the matches( ) method on the Matcher class object, which returns Boolean true / false for the result.

As you see in this type 2 code, we used the same pattern object for all three input strings. This makes the code a lot more efficient as we are compiling the pattern only once.

Hope this explanation is sufficient to learn regex in java. For further queries, please specify your queries in the comments. I love to solve them!

Don’t forget to follow us on Facebook and Twitter for regular updates.

Leave a Reply