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 subpackageimport java.util.regex.*;Â
// Main classpublic 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 subpackageimport java.util.regex.*;Â
// Main classpublic 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



