Java Regular Expression

Hello people! In this tutorial, we dig into the topic of Java Regex i.e. Java Regular Expression.

Java Regular Expression

Java Regex is a feature provided by the java.util.regex package in Java. The main use of Java Regex is to search, manipulate and validate strings based on patterns. This package mainly contains three classes i.e.

  • Matcher class:
    We use the Matcher class to perform match operations with a pattern that we detect from a sequence of characters, i.e., we use it to find a pattern.
  • Pattern class:
    The Pattern class represents a compiled regular expression, it also provides methods to create and work with regular expression patterns and the main use of it is to define a pattern.
  • PatternSyntaxException:
    The PatternSyntaxException class serves to highlight syntax errors encountered in regular expression patterns.

Each of these classes has many built-in methods that provide various functionalities related to regular expressions.

Java Code:

import java.util.regex.*;
public class Regex {
       public static void main(String[] args) {
        // TODO Auto-generated method stub
        Pattern p=Pattern.compile("codespeedy");
        Matcher m=p.matcher("I am an intern at codespeedy");
        boolean found=m.find();
        if(found) {
            System.out.println("Match found");
        }
        else {
            System.out.println("Match not found");
        }
        }
}

Let’s learn the code bit by bit!

import java.util.regex.*;
public class Regex {

Here we first imported the regex package this is a must as regex is not part of the Java language itself.

Pattern p=Pattern.compile("codespeedy");
Matcher m=p.matcher("I am an intern at codespeedy");

Here we have created an instance for the Pattern and Matcher class.

We create the pattern object by using the Pattern.compile() method, thus compiling the given regular expression into a pattern to search in the string. Pattern class also provide specifiers and flags which can simplify your searching experience some of which are 

Pattern.CASE_INSENSITIVE: This specifier suggests that the pattern needs to be matched ignoring the case of the characters.
and many more.
You can learn more about these specifiers here: Java specifiers and flags

In the Matcher instance, we are provided with a string to search the pattern we provided above. We used the p.matcher() method, which is a method of the Pattern class, to specify our string and the pattern to match it.

boolean found=m.find();
if(found) {
    System.out.println("Match found");
}
else {
    System.out.println("Match not found");
}

Here, we created a boolean variable ‘found’ and assigned it the result of ‘m.find()’, which returns true or false based on the search of the patterns; if not found, it returns false, otherwise true.
Based on the boolean value, it enters either the if block or the else block. For instance, if ‘m.find()’ returns true, meaning ‘found’ is true, it enters the if block and prints “Match found” .

Output:

Match found

Regular expressions in Java are really useful for string manipulation, validation and searching. I hope this tutorial gave you a basic understanding of the Java Regular Expression topic!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top