Learn Regular Expressions in Java

Regular expressions in Java

Learn Regular expressions in Java


Java provides regular expression packages that are useful for software developers and testers alike. Java regular expressions are varied, versatile and allow developers to define string patterns that can be used to search, edit or manipulate text. The java.util.regex API comprises multiple classes each of which has a specific responsibility. For example, the java.util.regex pattern is used for defining patterns while the java.util.regex.Matcher is used for matching operations based on the patterns defined by the java.util.regex.Pattern class. It is just about using the appropriate class for the purpose.

In this article, we will check how regular expressions can be used in Java applications.

Do you know – Steps to work with Java Persistence API? 

Why we need regular expression?

Regular expressions can be immensely useful especially in large software development projects with long lines of code. In such projects, software developers need to do several things such as creating reusable blocks of codes; refactoring or cleaning codes and putting comments for codes. Now, imagine doing that manually over a large codebase. That would be a colossal waste of time and productivity. This is where regular expressions are so helpful. Software developers or even the testers can define specific regular expressions for specific purposes and just reuse the regular expression. For example, if the developer wants to search for a term “resource” across the code base without the search being case-sensitive, then the developer could define a regular expression accordingly like the following:

String content = “This is a resource product”;

String patternString = “.*reSoUrCe.”;

Pattern pattern=Pattern.compile (patternString, Pattern.CASE_INSENSITIVE);

Re-usability and efficiency are two of the main advantages of having a regular expression. The great thing is that multiple regular expressions can be defined depending on the purpose.

Must Read – Steps to work with Java generics? 

Components of Java regular expression package

Before we start writing regular expressions in Java, we should have a clear understanding of the components available in the java.util.regex package.

There are mainly three classes available in the package. Let’s have a look at what they actually do.

Pattern class: Regular expressions are generally represented as a sequence of characters/symbols/special characters etc. So, the basic process is to make it an object before it is usable. Once the regular expression is compiled, it becomes a pattern object. Pattern class does not have any public constructor. It has a public static void method called ‘compile’. Regular expression is passed to this method as an argument and the return object is a Pattern object. As we have used in our sample application.

Pattern test_pattern = Pattern.compile (“\\s+”, Pattern.CASE_INSENSITIVE);

Also read – JSON data and Java – How it works? 

Matcher class: Now, once the Pattern object is created, we need to have some mechanism to compare the input string with the Pattern. Here comes the role of Matcher class, it acts as a regex engine to match the input and the Pattern. It does not have any public constructor, rather the matcher method of Pattern object is used to take the input and return a Matcher object. After this the ‘matches’ method of Matcher class is used to get the actual result, it returns a Boolean value indicating whether the result is true (matched) or false (not matched).

Following example shows the implementation.

Listing 1: Testing Regex components in Java


package demo.techalpine.com;

import java.util.regex.*;

public class RegExComponents {

public static void main(String[] args) {

//Creating Pattern object

Pattern tst_pattern = Pattern.compile(“.duoni.”);

//Creating Matcher object

Matcher tst_matcher = tst_pattern.matcher(“TechAlpine”);

//Testing input string

System.out.println(“Checking…if input string matches with the pattern – “+tst_matcher.matches());




Now compile and run the class.

PatternSyntaxException class: This is the exception class used for detecting any syntax error.

Environment setup:

To configure the environment following are the four steps to be followed

  • Download JDK from the following link and set the class path


Now your environment is set to write regular expressions in Java. In the next section we will create one sample application.

You will like to read – Creating RESTful web service in Java 

Let’s try a sample application:

In this section we will write a sample application for testing regular expression. Following screen shot shows the project structure for the application.

In this example we will have a simple string with white spaces. This string will bet the input to the matcher. Now the objective is to remove the white spaces and replace it with ‘**’ string.

We will create a Pattern class and a Matcher class. After this the input string will be passed to check with the pattern. We will also display the indexes of the white spaces in the string.

Follow the project structure in the Eclipse environment. Once the program is written, compile and run it. 

Listing 2: Sample application


package demo.techalpine.com;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class RegDemo {

// Creating sample string for testing

public static final String SAMPLE_STRING = “Let us check regular expression in Java”;

public static void main(String[] args) {

Pattern test_pattern = Pattern.compile(“\\s+”, Pattern.CASE_INSENSITIVE);

Matcher test_matcher = test_pattern.matcher(SAMPLE_STRING);

//Let us print the matcher

System.out.println(“Printing the matcher:- “+ test_matcher+ “\n”);

//Let us check matching indexes

System.out.println(“Printing indexes:- “+”\n”);

while (test_matcher.find()) {

System.out.print(“Starting index: ” + test_matcher.start());

System.out.print(“Closing index: ” + test_matcher.end() + “\n”);


//Now compiles the pattern

Pattern replace_pattern = Pattern.compile(“\\s+”);

//Matching the pattern with the sample string

Matcher second_matcher = replace_pattern.matcher(SAMPLE_STRING);

//Printing original string

System.out.println(“\n”+”Printing original string:- “+”\n”);


//Now replace the spaces with ** character

System.out.println(“\n”+”Printing the modified string:- “+”\n”);





Now compile and run the application.

Read – Learn Log4j – Logging API for Java 

Conclusion: In this article we have discussed different components of Java regular expression package. We have also checked how it works and why it is important. Regular expressions are used in different scenarios like password validation, email address validation etc. The basic idea is to create a pattern as per the requirement and then validate the input. This is what regular expressions actually do. It reduces the development time and creates reusable components. So it is very helpful to learn regular expressions and then create it as per need.

Must Read- All Java articles 


============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric

Enjoy this blog? Please spread the word :)

Follow by Email