Java Matcher find() 方法

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

find 方法按字符搜索指定模式或表达式在给定子序列中,如果找到则返回 true,否则返回 false。

签名

Java 中 find 方法有两种。find 方法的签名如下

编号。方法描述
1boolean find()如果在输入序列中找到匹配项,则返回 true。
2boolean find(int start)如果通过起始索引在输入序列中找到匹配项,则返回 true。

参数

start - 开始搜索匹配项的索引

返回值

如果从给定索引开始的输入序列的子序列匹配此匹配器的模式,则返回 true

抛出

IndexOutOfBoundsException - 如果 start 小于零或大于输入序列的长度。

示例 1

输出

Start position of matching String 5
End position of Matching String (java) 9
Start position of matching String 14
End position of Matching String (java) 18
Number of matches : 2

示例 2

输出

Start position of matching String (java) 14
End position of Matching String (java) 18
Number of matches: 1