Learn How to Work With Regular Expressions in Java

0
1306
regular-expression

regular-expression

Java provides regular expressions package 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 includes 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 job.

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

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:

Reusability 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.

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. An example that we have used in our sample application:

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 the 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).

The following example shows the implementation:
Listing 1: Testing Regex components in Java

package demo.eduonix.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("Eduonix");
		//Testing input string
		System.out.println("Checking...if input string matches with the pattern - "+tst_matcher.matches());
	}
}

Now compile and run the class. Following will be the output result.
testing-regex-components
Figure 1: Testing Regex components.

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

Environment Setup: To configure the environment, follow these four steps:

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

Let’s try a sample application:
In this section, we will write a sample application for testing regular expression. The following screen shot shows the project structure for the application.
In this example, we 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.
Following is the project structure in the Eclipse environment. Once the program is written, compile and run it.
project-structure
Figure2: Project structure

Listing 2: Sample application

package demo.eduonix.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");
            System.out.println(SAMPLE_STRING);
            //Now replace the spaces with ** character
            System.out.println("\n"+"Printing the modified string:- "+"\n");
            System.out.println(second_matcher.replaceAll("**"));
    }
}

Now compile and run the application. Following will be the output result.

output-result
Figure3: Output result

Conclusion: In this article we have discussed different components of the 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. Hence, it is very helpful to learn regular expressions and then create it as per need.

LEAVE A REPLY

Please enter your comment!
Please enter your name here