📜  Java中的 Matcher usePattern(Pattern) 方法及示例

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

Java中的 Matcher usePattern(Pattern) 方法及示例

Matcher 类usePattern()方法用于获取该匹配器要匹配的模式。句法:

public Matcher usePattern(Pattern newPattern)

参数:此方法接受一个参数newPattern ,它是要设置的新模式。返回值:此方法返回一个带有新模式的匹配器异常:如果 newPattern 为 null,此方法将引发IllegalArgumentException 。下面的示例说明了 Matcher.usePattern() 方法:示例 1:

Java
// Java code to illustrate usePattern() method
 
import java.util.regex.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Get the regex to be checked
        String regex = "Geeks";
 
        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);
 
        // Get the String to be matched
        String stringToBeMatched
            = "GeeksForGeeks";
 
        // Create a matcher for the input String
        Matcher matcher
            = pattern
                  .matcher(stringToBeMatched);
 
        // Get the new Pattern
        String newPattern = "GFG";
 
        // Get the Pattern using pattern method
        System.out.println("Old Pattern: "
                           + matcher.pattern());
 
        // Set the newPattern using usePattern() method
        matcher = matcher
                      .usePattern(
                          Pattern
                              .compile(newPattern));
 
        // Get the Pattern
        // using pattern method
        System.out.println("New Pattern: "
                           + matcher.pattern());
    }
}


Java
// Java code to illustrate usePattern() method
 
import java.util.regex.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Get the regex to be checked
        String regex = "GFG";
 
        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);
 
        // Get the String to be matched
        String stringToBeMatched
            = "GFGFGFGFGFGFGFGFGFG";
 
        // Create a matcher for the input String
        Matcher matcher
            = pattern
                  n.matcher(stringToBeMatched);
 
        // Get the new Pattern
        String newPattern = "Geeks";
 
        // Get the Pattern using pattern method
        System.out.println("Old Pattern: "
                           + matcher.pattern());
 
        // Set the newPattern using usePattern() method
        matcher = matcher
                      .usePattern(
                          Pattern
                              .compile(newPattern));
 
        // Get the Pattern
        // using pattern method
        System.out.println("New Pattern: "
                           + matcher.pattern());
    }
}


输出:
Old Pattern: Geeks
New Pattern: GFG

示例 2:

Java

// Java code to illustrate usePattern() method
 
import java.util.regex.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Get the regex to be checked
        String regex = "GFG";
 
        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);
 
        // Get the String to be matched
        String stringToBeMatched
            = "GFGFGFGFGFGFGFGFGFG";
 
        // Create a matcher for the input String
        Matcher matcher
            = pattern
                  n.matcher(stringToBeMatched);
 
        // Get the new Pattern
        String newPattern = "Geeks";
 
        // Get the Pattern using pattern method
        System.out.println("Old Pattern: "
                           + matcher.pattern());
 
        // Set the newPattern using usePattern() method
        matcher = matcher
                      .usePattern(
                          Pattern
                              .compile(newPattern));
 
        // Get the Pattern
        // using pattern method
        System.out.println("New Pattern: "
                           + matcher.pattern());
    }
}
输出:
Old Pattern: GFG
New Pattern: Geeks

参考: Oracle 文档