📜  Java中的MatchResult接口

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

Java中的MatchResult接口

Java中的接口用于实现抽象。它也被视为课程的蓝图。接口只能包含抽象方法,但它可能包含也可能不包含变量。换句话说,接口只能包含抽象方法和变量。 Java仅支持通过接口进行多重继承。因此,接口在Java中具有更广泛的范围。

本文重点介绍 MatchResult 接口。

匹配结果接口:

Java中的MatchResult接口表示匹配操作的结果或结论。它包含某些查询方法的定义,可用于确定与正则表达式匹配的结果。请注意,我们无法使用 MatchResult 界面更改组/组边界和匹配边界,但可以通过该界面轻松查看它们。此接口是在Java 1.5 版中引入的。

可以使用以下语法声明此接口,

句法:

public interface MatchResult

MatchResult 接口的方法

MatchResult 接口的查询方法将在下面详细讨论。

1. start() 方法:该方法用于返回匹配的开始索引。

句法:

int start()

返回值:

  • integer:表示第一个匹配的字符的索引

例子:

Java
// Java program to illustrate the working of start() method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Initializing regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main method
    public static void main(String[] args)
    {
        // Compiling the given regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Applying matcher operation
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
 
            // Retrieve the MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Printing the starting index of the match
            System.out.println(
                "Starting index of the match: "
                + myResult.start());
        }
    }
}


Java
// Java program to illustrate the working
// of start(int index) method
 
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class GFG {
 
    // Initializing the regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing the input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main method
    public static void main(String[] args)
    {
        // Compiling the regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Applying matcher operation
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
 
            // Retrieve the MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the start index of the sequence
            // that was held by the given group during the
            // match.
            System.out.println("Second Capturing Group: "
                               + myResult.start(1));
        }
    }
}


Java
// Java program to illustrate the working of end() method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Initializing the regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing the input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main method
    public static void main(String[] args)
    {
        // Compiling the regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Applying the matcher operation
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
            // Retrieve the MatchResult object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the offset following the last
            // character matched
            System.out.println("First Capturing Group: "
                               + myResult.end());
        }
    }
}


Java
// Java program to illustrate the working
// of end(int index) method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    public static void main(String[] args)
    {
        // Compiling regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // applying matcher operation
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
            // get the MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the offset after the last
            // character of the sequence held by
            // the given group at the time of match.
            System.out.println("Second Capturing Group: "
                               + myResult.end(1));
        }
    }
}


Java
// Java program to illustrate the working of group() method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Initializing regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main() method
    public static void main(String[] args)
    {
        // Compiling regular expression
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Initiating the matcher object
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
 
            // Initiating the MatchResult object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the input sequence matched by the
            // previous match.
            System.out.println("First Capturing Group"
                               + myResult.group());
        }
    }
}


Java
// Java program to illustrate the working
// of group(int index) method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Initializing regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main method
    public static void main(String[] args)
    {
        // Compiling regular expression
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Instantiating matcher object
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
            // Instantiating MatchResult object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the input subsequence that was held
            // by the given group during the last match
            // occurred.
            System.out.println(
                "Second Capturing Group - Match String: "
                + myResult.group(1));
        }
    }
}


Java
// Java program to illustrate the working
// of groupCount() method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Regular expression
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    public static void main(String[] args)
    {
        // Compiling regular expression
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Instantiating a matcher object
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
            // Instantiating a MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the number of capturing groups in this
            // match result's pattern.
            System.out.println(
                "The number of capturing groups is equal to: "
                + myResult.groupCount());
        }
    }
}


输出
Start index of the match: 0

2. start(int group):该方法返回匹配期间给定组捕获的序列的起始索引。

句法:

int start(int index)  

参数:

  • index:它表示匹配器模式中捕获组的索引。

返回值:

  • 非负整数:表示组持有的第一个字符的索引
  • -1:仅当匹配成功但该组不匹配任何内容时

例子:

Java

// Java program to illustrate the working
// of start(int index) method
 
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class GFG {
 
    // Initializing the regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing the input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main method
    public static void main(String[] args)
    {
        // Compiling the regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Applying matcher operation
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
 
            // Retrieve the MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the start index of the sequence
            // that was held by the given group during the
            // match.
            System.out.println("Second Capturing Group: "
                               + myResult.start(1));
        }
    }
}
输出
Second Capturing Group: 0

3. end():该方法返回最后一个字符匹配时的偏移量。

句法:

int end() 

返回类型:返回最后一个匹配字符后的偏移量。

例子:

Java

// Java program to illustrate the working of end() method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Initializing the regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing the input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main method
    public static void main(String[] args)
    {
        // Compiling the regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Applying the matcher operation
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
            // Retrieve the MatchResult object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the offset following the last
            // character matched
            System.out.println("First Capturing Group: "
                               + myResult.end());
        }
    }
}
输出
First Capturing Group: 43

4. end(int index):该方法返回最后一个字符匹配时的偏移量。

句法:

int end(int index)

范围:

  • index:它表示匹配器模式中捕获组的索引。

返回类型:返回最后一个匹配字符后的偏移量。

例子:

Java

// Java program to illustrate the working
// of end(int index) method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    public static void main(String[] args)
    {
        // Compiling regular expression string
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // applying matcher operation
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
            // get the MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the offset after the last
            // character of the sequence held by
            // the given group at the time of match.
            System.out.println("Second Capturing Group: "
                               + myResult.end(1));
        }
    }
}
输出
Second Capturing Group: 18

5. group():该方法返回与上一次匹配匹配的输入序列。

句法:

String group()

返回类型:

  • A 字符串:上一个匹配匹配的序列。

例子:

Java

// Java program to illustrate the working of group() method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Initializing regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main() method
    public static void main(String[] args)
    {
        // Compiling regular expression
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Initiating the matcher object
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
 
            // Initiating the MatchResult object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the input sequence matched by the
            // previous match.
            System.out.println("First Capturing Group"
                               + myResult.group());
        }
    }
}
输出
First Capturing GroupHello World!, 41346, these are the numbers.

6. group(int index):该方法用于返回指定组在上一次匹配操作时所持有的输入序列。

句法:

int group(int index)

参数:

  • index:在此匹配器模式中捕获的组的索引。

返回类型:

  • A 字符串:它表示在上一次匹配时已经被组捕获的序列
  • null:如果组不能匹配输入的任何部分。

例子:

Java

// Java program to illustrate the working
// of group(int index) method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Initializing regular expression string
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Initializing input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    // Main method
    public static void main(String[] args)
    {
        // Compiling regular expression
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Instantiating matcher object
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
            // Instantiating MatchResult object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the input subsequence that was held
            // by the given group during the last match
            // occurred.
            System.out.println(
                "Second Capturing Group - Match String: "
                + myResult.group(1));
        }
    }
}
输出
Second Capturing Group - Match String: Hello World!, 4134

7. groupCount():该方法指定匹配结果模式中捕获组的数量。

句法:

int groupCount()

返回类型:

  • integer:它表示匹配器模式中捕获组的数量。

例子:

Java

// Java program to illustrate the working
// of groupCount() method
 
import java.io.*;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class GFG {
 
    // Regular expression
    private static final String regularExpression
        = "(.*)(\\d+)(.*)";
 
    // Input string
    private static final String input
        = "Hello World!, 41346, these are the numbers.";
 
    public static void main(String[] args)
    {
        // Compiling regular expression
        Pattern myPattern
            = Pattern.compile(regularExpression);
 
        // Instantiating a matcher object
        Matcher myMatcher = myPattern.matcher(input);
 
        if (myMatcher.find()) {
            // Instantiating a MatchResult Object
            MatchResult myResult
                = myMatcher.toMatchResult();
 
            // Prints the number of capturing groups in this
            // match result's pattern.
            System.out.println(
                "The number of capturing groups is equal to: "
                + myResult.groupCount());
        }
    }
}
输出
The number of capturing groups is equal to: 3