java中的接口就是用来实现抽象的。它也被认为是类的蓝图。接口只能包含抽象方法,但它可能包含也可能不包含变量。换句话说,接口只能包含抽象方法和变量。 Java 仅通过接口支持多重继承。因此,接口在 Java 中具有更广泛的范围。
本文重点介绍MatchResult 接口。
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());
}
}
}
Start index of the match: 0
2. start(int group):该方法返回给定组在比赛期间捕获的序列的起始索引。
用法:
int start(int index)
参数:
- index:它表示匹配器模式中捕获组的索引。
返回值:
- non-negative integer:它表示该组所持有的第一个字符的索引
- -1:仅当匹配成功但组don-not匹配任何内容时
例子:
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()
返回类型:
- 一个字符串:与上一个匹配项匹配的序列。
例子:
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: 在此匹配器模式中捕获的组的索引。
返回类型:
- 一个字符串:它代表上一场比赛时该组捕获的序列
- 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
相关用法
- Java MatchResult end()用法及代码示例
- Java MatchResult end(int)用法及代码示例
- Java MatchResult group()用法及代码示例
- Java MatchResult groupCount()用法及代码示例
- Java MatchResult group(int)用法及代码示例
- Java MatchResult start()用法及代码示例
- Java MatchResult start(int)用法及代码示例
- Java Matcher appendReplacement()用法及代码示例
- Java Matcher appendTail()用法及代码示例
- Java Matcher quoteReplacement()用法及代码示例
- Java Matcher region()用法及代码示例
- Java Matcher useAnchoringBounds()用法及代码示例
- Java Matcher useTransparentBounds()用法及代码示例
- Java Matcher appendTail(StringBuffer)用法及代码示例
- Java Matcher appendTail(StringBuilder)用法及代码示例
- Java Matcher end()用法及代码示例
- Java Matcher end(int)用法及代码示例
- Java Matcher find()用法及代码示例
- Java Matcher find(int)用法及代码示例
- Java Matcher group()用法及代码示例
- Java Matcher groupCount()用法及代码示例
- Java Matcher group(String)用法及代码示例
- Java Matcher hasAnchoringBounds()用法及代码示例
- Java Matcher hasTransparentBounds()用法及代码示例
- Java Matcher hitEnd()用法及代码示例
注:本文由纯净天空筛选整理自bhuwanesh大神的英文原创作品 MatchResult Interface in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。