Friday, October 10, 2025
HomeLanguagesJavaMatcher reset() Method in Java with Examples

Matcher reset() Method in Java with Examples

The reset() method of Matcher Class is used to reset this matcher, in order to understand it better it is recommended to have prior knowledge of Pattern and Matcher class in java regex sub-package. Here we will be illustrating it with help of Java programs. 

Syntax: 

public Matcher reset()

Parameters: This method does not take any parameters.

Return Value: This method returns this Matcher after being reset.

Example 1:

Java




// Java Program to illustrate reset() method
// of Matcher class
 
// Importing class from java.util.regex package
// where regex is a subpackage
import java.util.regex.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Getting the regex to be checked
        // taking via string input
        String regex = "Geeks";
 
        // Creating a pattern from regex
        // using Pattern class
        Pattern pattern = Pattern.compile(regex);
 
        // Getting the String to be matched
        String stringToBeMatched = "GeeksForGeeks";
 
        // Creating a matcher for the input String
        // using Matcher class
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
 
        // Resetting the Matcher using reset() method
        matcher = matcher.reset();
 
        // Getting the current matcher state
        // using tomatchResult() method and
        // printing it
        System.out.println(matcher.toMatchResult());
    }
}


Output

java.util.regex.Matcher$ImmutableMatchResult@448139f0

Example 2:

Java




// Java Program to Illustrate reset() method
// of Matcher class
 
// Importing class from java.util.regex package
// where regex is a subpackage
import java.util.regex.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Getting the regex to be checked
        String regex = "GFG";
 
        // Creating a pattern from regex
        Pattern pattern = Pattern.compile(regex);
 
        // Getting the String to be matched
        String stringToBeMatched = "GFGFGFGFGFGFGFGFGFG";
 
        // Creating a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
 
        // Resetting the Matcher
        // using reset() method
        matcher = matcher.reset();
 
        // Getting the current matcher state
        // using toMatchResult() method
        System.out.println(matcher.toMatchResult());
    }
}


Output

java.util.regex.Matcher$ImmutableMatchResult@448139f0
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS