Open In App

java.util.regex Package

Last Updated : 31 Oct, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The java.util.regex package in Java provides classes for matching character sequences against regular expressions. It allows developers to define complex search patterns and apply them to text for operations such as validation, searching, replacement, and splitting strings.

Package Declaration

Every class that uses regex features should import this package as follows:

import java.util.regex.*;

Java
import java.util.regex.*;

public class GFG {

    public static void main(String[] args)
    {

        // Define a regex pattern for digits
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher
            = pattern.matcher("Java 17 and 2025 version");

        while (matcher.find()) {
            System.out.println("Found number: "
                               + matcher.group());
        }
    }
}

Output
Found number: 17
Found number: 2025

Explanation:

  • The regex \\d+ matches one or more digits.
  • The Matcher object searches the input string and extracts all matching numeric sequences.

Key Classes Regex Package

1. Pattern Class

The Pattern class is used to compile a regular expression into a pattern object. It is immutable and thread-safe, meaning the same pattern can be safely used by multiple threads.

Syntax:

Pattern pattern = Pattern.compile("regex");

Java
import java.util.regex.*;

public class GFG {

    public static void main(String[] args){

        // matches one or more digits
        String regex = "\\d+";
        String input = "Order1234";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        System.out.println("Does input contain digits? "
                           + matcher.find());
    }
}

Output
Does input contain digits? true

Methods of Pattern classs

2. Matcher Class

The Matcher class is used to perform match operations such as finding, matching, and replacing patterns in text.

Syntax:

Matcher matcher = pattern.matcher(input);

Java
import java.util.regex.*;

public class GFG{
    
    public static void main(String[] args){
        
        String regex = "\\bJava\\b";
        String input = "I love Java and Java is powerful.";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        int count = 0;
        while (matcher.find()) {
            count++;
            System.out.println("Match found at: " + matcher.start() + " - " + matcher.end());
        }

        System.out.println("Total Matches: " + count);
    }
}

Output
Match found at: 7 - 11
Match found at: 16 - 20
Total Matches: 2

Methods of Matcher classs

3. PatternSyntaxException Class

The PatternSyntaxException is thrown when there’s an invalid syntax in the regular expression. It helps identify the error index and description for debugging.

Syntax:

PatternSyntaxException exception = new PatternSyntaxException(description, pattern, index)

Java
import java.util.regex.*;

public class GFG {

    public static void main(String[] args){

        try{
            Pattern pattern = Pattern.compile(
                "[a-z"); // Missing closing bracket
        }
        catch (PatternSyntaxException e) {
            System.out.println("Description: "
                               + e.getDescription());
            System.out.println("Error Index: "
                               + e.getIndex());
            System.out.println("Invalid Pattern: "
                               + e.getPattern());
        }
    }
}

Output
Description: Unclosed character class
Error Index: 3
Invalid Pattern: [a-z

Methods of PatternSyntaxException Class

Common Regex Examples

PurposeRegex PatternExample InputMatches
Digits only\\d+Order123123
Alphabets only[a-zA-Z]+Hello123Hello
Email validation^[\\w.-]+@[\\w.-]+\\.\\w+$user@mail.comValid
Mobile number (10 digits)\\d{10}9876543210Valid
Starts with "Java"^Java.*JavaProgrammingValid



Article Tags :

Explore