📌  相关文章
📜  Java中的 Matcher reset() 方法及示例

📅  最后修改于: 2022-05-13 01:55:08.641000             🧑  作者: Mango

Java中的 Matcher reset() 方法及示例

Matcher 类reset()方法用于重置这个匹配器,为了更好的理解,建议先了解Java regex 子包中的Pattern 和Matcher 类。在这里,我们将借助Java程序对其进行说明。

句法:

public Matcher reset()

参数:此方法不带任何参数。

返回值:此方法在重置后返回此Matcher

示例 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);
 
        // Reseting the Matcher using reset() method
        matcher = matcher.reset();
 
        // Getting the current matcher state
        // using tomatchResult() method and
        // printing it
        System.out.println(matcher.toMatchResult());
    }
}


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());
    }
}


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

示例 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());
    }
}
输出
java.util.regex.Matcher$ImmutableMatchResult@448139f0