他们有什么方法可以找到匹配器 class 的 find 方法的大小吗?在 java

Is their any method to find the size of find method of matcher class?? in java

我必须找到匹配子串的大小。

例如

string s="2---3"
     Pattern p=Pattern.compile("-+");
 Matcher m=p.matcher(lst_str.get(i));
if(m.find()) // answer  is 3* 


if String s="2--2" // then answer is 2

如何找到匹配的子字符串的大小?

只需使用每个字符串匹配的长度属性:

String s = "2---3";
Pattern p = Pattern.compile("-+");
Matcher m = p.matcher(s);
while (m.find()) {
    System.out.println("MATCH: " + m.group(0) + ", with length = " + m.group(0).length());
}

如果你一次只需要做一个字符串,并且你想要一个单行,这里有一个方法:

String s = "2---3";
int lengthOfMatch = s.length() - s.replaceAll("-+", "").length();