📜  Java中的匹配器类

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

Java中的匹配器类

在Java中,Matcher 是由 MatchResult 接口实现的类,它通过解释 Pattern 对字符序列执行匹配操作。

下面,我们可以看到Java .lang.Object类中 java Java的声明:

public final class Matcher extends Object implements MatchResult

通过调用模式的 matcher 方法,可以从模式创建匹配器。如果匹配器被创建一次,我们可以对其执行三种不同的匹配操作:

  • matches() 尝试将总输入序列与模式匹配。
  • lookingAt() 尝试匹配输入序列,与模式相匹配,从头开始。
  • find() 这会扫描输入序列并查找下一个子序列,特别是与模式匹配。

Matcher 类的方法

为了方便起见,下表中的 Matcher 类的方法根据其功能进行了分组。

一、索引方法:

它提供了有用的索引值。它准确显示是否在输入字符串中找到匹配项:

S. No.Method NameDescription
1public int start()This method returns the start index of the previous match.
2public int start(int group)This method returns the start index of the subsequence captured by the given group during the previous match operation.
3public int end()This method returns the offset after the last character is matched.
4public int end(int group)This method returns the offset after the last character of the subsequence captured by the given group during the previous match operation.

2.学习方法:

它检查输入字符串并返回一个布尔值,指示是否找到了模式:

S. No.Method NameDescription
1public boolean lookingAt()This method aims to match the input sequence, starting at the beginning of the region, against the pattern.
2public boolean find()This method aims to find the next subsequence of the input sequence that matches the pattern.
3public boolean find(int start)Resets this matcher and then tries to find the next subsequence of the input sequence which matches the pattern, starting at the specified index.
4public boolean matches()This method aims to match the entire region against the pattern.

三、更换方法:

这些是替换输入字符串中文本的有用方法:

S. No.Method NameDescription
1public Matcher appendReplacement(StringBuffer sb, String replacement)This method implements a non-terminal append-and-replace step.
2public StringBuffer appendTail(StringBuffer sb)This method implements a terminal append-and-replace step.
3public String replaceAll(String replacement)This method replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
4public String replaceFirst(String replacement)This method replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.
5public static String quoteReplacement(String s)This method returns a literal replacement String for the specified String, this method also produces a String which will work in the appendReplacement method as a literal replacement of the Matcher class.

示例 1:这里我们可以看到示例 GFG。 Java使用start()end()计算单词“geek”出现在输入字符串中的次数:

Java
// Java program to demonstrate the
// methods of Matcher class in Java
  
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class GFG {
  
    private static final String REGEX = "\\bgeek\\b";
    private static final String INPUT
        = "geek geek geek geekie geeks";
  
    public static void main(String[] args)
    {
        Pattern pat = Pattern.compile(REGEX);
        
        //  here get a matcher object
        Matcher mat = pat.matcher(INPUT);
        
        // initialize a count variable to count
        int count = 0;
  
        // try to match the entire input sequence against
        // the pattern using the loop
        while (mat.find()) {
            count++;
            System.out.println("Match number " + count);
            System.out.println("start(): " + mat.start());
            System.out.println("end(): " + mat.end());
        }
    }
}


Java
// Java program to demonstrate the
// methods of Matcher class in Java
  
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class GFG {
  
    private static final String REGEX = "geek";
    private static final String INPUT = "geeksforgeeks";
    private static Pattern pat;
    private static Matcher mat;
  
    public static void main(String[] args)
    {
  
        // Initialization for pattern and matcher
        pat = Pattern.compile(REGEX);
        mat = pat.matcher(INPUT);
  
        System.out.println("Current REGEX: " + REGEX);
        System.out.println("Current INPUT: " + INPUT);
  
        System.out.println("lookingAt(): "
                           + mat.lookingAt());
        System.out.println("matches(): " + mat.matches());
    }
}


输出
Match number 1
start(): 0
end(): 4
Match number 2
start(): 5
end(): 9
Match number 3
start(): 10
end(): 14

示例 2:在此示例中,我们可以看到 GFG。 Java, lookingAt()matches()都尝试将输入序列与模式匹配。

Java

// Java program to demonstrate the
// methods of Matcher class in Java
  
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class GFG {
  
    private static final String REGEX = "geek";
    private static final String INPUT = "geeksforgeeks";
    private static Pattern pat;
    private static Matcher mat;
  
    public static void main(String[] args)
    {
  
        // Initialization for pattern and matcher
        pat = Pattern.compile(REGEX);
        mat = pat.matcher(INPUT);
  
        System.out.println("Current REGEX: " + REGEX);
        System.out.println("Current INPUT: " + INPUT);
  
        System.out.println("lookingAt(): "
                           + mat.lookingAt());
        System.out.println("matches(): " + mat.matches());
    }
}
输出
Current REGEX: geek
Current INPUT: geeksforgeeks
lookingAt(): true
matches(): false