Open In App

Matcher reset() Method in Java with Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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


Last Updated : 25 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads