Tuesday, November 19, 2024
Google search engine
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

Recent Comments