Java

How to write regex in Java

The Regex (acronym of the regular expression) is a sequence of characters that assist us in searching and manipulating the strings. Regular expressions can be used in all programming languages such as java, c++, etc. To avail the functionalities of the regex, java provides a built-in package java.util.regex which contains three classes Pattern, Matcher, and PatternSyntaxException.

This write-up will provide a comprehensive overview of how to write regex in java and to do so we have to understand the following concepts:

So, let’s get started!

java.util.regex Package

It is a build-in package that includes three classes: a Pattern class to define a pattern that will be used in searching, a Matcher class that can be used to search for a pattern, and a PatternSyntaxException class to indicate a syntax error/exception in a regex pattern.

Pattern Creation and Pattern Matching in Regex

We have to invoke the compile() method of the Pattern class to create a pattern. The compile() method will return a pattern which will be interpreted by the matcher() method to perform the match operation over the String/sequence of characters.

Before heading toward how to write a regex in java first we need to understand some important flags, metacharacters, and quantifiers that can be helpful in writing a regex.

Patterns in Regex

Pattern class utilizes its compile() method to create a pattern and it can take two parameters, one for the pattern to be searched and the other one is optional which is used to specify a flag.

As the compile() method takes a pattern in the first parameter, therefore some frequently used patterns are provided below:

  1. [—] within brackets the “—” represents various characters, while [—] represents a pattern which defines that find/search a character among the options specified within the brackets.
  2. [^—] here the ^ sign represents not, which means if we specify a ^ sign with some pattern then it means find a character except the options specified within the brackets.
  3. [0-9], represents finding a number between 0 to 9.

Flags in Regular Expressions

Flags can be passed as the second parameter to the compile() method and it is used to specify the searching behavior of the regex. For instance, CASE_INSENSTIVE is a flag that determines: search for the pattern irrespective of the case sensitivity i.e. uppercase or lowercase.

MetaCharacters in Regex

The metacharacters assist us in defining the searching criteria for the regex and these Metacharacters have some special meaning. Some most frequently used metacharacters are listed in the below-given table:

MetaCharacters Description
\d finds a digit
\b finds a match at the beginning or at the end of the word
$ finds a match at the end of the string
^ finds the match at the start of the string
\s Used to search the white spaces
| Finds a match from multiple options that are separated with ‘|’.
. Used to match a single character

 

Quantifiers in Regex

As the name itself suggests, the Quantifiers specifies quantity i.e. how many times a number occurs in a pattern, some frequently used quantifiers are provided in the following table:

Quantifiers Description
A+ A occurs at least one time
A* A occurs zero or more time
A? A occurs either zero time or one time
A{n} A occurs n number of times
A{n,} A occurs n times or more than n times
A{x,y} A occurs between the provided range i.e. A occurs at least x times but less than y times

How to Write Regex in Java

The pattern class will be used to create a pattern and to do so we have to invoke its “public static compile()” method, afterward we will specify the pattern within the compile() method.

In Java, a regex can be written in multiple ways as listed below:

First Method

In the below snippet, firstly, we create the object of Pattern class and we specify the pattern within its compile method. We utilize a metacharacter “.” which says that the string consists of two letters where the first letter can be anything but the second letter must be “i”:

public class WriteRegexExample {
publicclassWriteRegexExample {
publicstaticvoidmain(String args[]) {
        Pattern ptrn = Pattern.compile(".i");
        Matcher mat = ptrn.matcher("hi");
booleanbol = mat.matches();
System.out.println(bol);
    }
}

We create the object of the Matcher class that will interpret the pattern and will perform the matching operations on the input string. Next, we utilized the matches() method which will return a boolean value, if the match is found it returns “true” else false.

The output shows true which means the desired match was found successfully.

Second Method

The below-given snippet shows another way of writing the regex in java:

boolean bol=Pattern.compile(".i").matcher("hi").matches();

System.out.println(bol);

This time we utilize the compile(), matcher(), and matches() methods in one go.

The output shows that our program is working properly.

Third Method

Another way to write a regex is we can directly utilize the matches() method of the matcher class with the Pattern class and within the parenthesis, we will specify the “pattern” and the string that we want to match with.

boolean bol = Pattern.matches(".i","hi");

System.out.println(bol);

The complete code and respective output are shown in the following snippet:

Output authenticates the working of the above program.

Conclusion

The regex is a sequence of characters that defines the search patterns that can be used to search, manipulate and edit a string. The regex can be written in multiple ways, the approach for writing a regex can be different, however, we have to utilize the compile() method of Pattern class, and matches() method of Matcher class in each approach. This write-up presents a detailed overview of how to write a regex in java and for a better understanding, it explains all the necessary aspects of regular expressions such as quantifiers, metacharacters, and so on.

 

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.