如何在Java正则表达式的同一行中捕获多个匹配项
示例
import java.util.regex.*;
class PatternMatcher {
public static void main(String args[]) {
int count = 0;
// String to be scanned to find the pattern.
String content = "aaa bb aaa";
String string = "aaa";
// Create a Pattern object
Pattern p = Pattern.compile(string);
// get a matcher object
Matcher m = p.matcher(content);
while(m.find()) {
count++;
System.out.println("Match no:"+count);
System.out.println("Found at: "+ m.start()+ " - " + m.end());
}
}
}输出结果
Match no:1 Found at: 0 - 3 Match no:2 Found at: 7 - 10
注意
start()–此方法用于获取使用find()方法找到的匹配项的起始索引。
end()–此方法用于获取使用find()方法找到的匹配项的结束索引。它返回最后一个匹配字符旁边的字符索引。
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短