Java Matcher start() 方法

2024 年 11 月 9 日 | 阅读 2 分钟

此方法用于返回上一个匹配项的开始索引。要返回第一个匹配字符的偏移量,我们需要使用 find 方法逐个字符匹配模式。

签名

Java 中有 3 种 start 方法。start 方法的签名如下。

编号。方法方法名称
1int start()此方法返回第一个字符匹配后的偏移量。
2int start(int group)此方法通过匹配由给定组捕获的子序列的起始字符来返回偏移量。
3int start(String name)此方法通过匹配由给定组名捕获的子序列的起始字符来返回偏移量。

指定者

MatchResult 接口中的 start

返回值

匹配的第一个字符的索引

抛出

IllegalStateException - 如果尚未尝试匹配,或者如果之前的匹配操作失败

示例 1

输出

Start position :1, End position : 4
Start position :4, End position : 7
Start position :7, End position : 10

示例 2

输出

start position of Matching String java 2

示例 3

输出

Start position of matching 5