结束例如算一个字符出现的所有除了在字符串
String text = "sentence"; // (number of e's = 2)
有三名辰在该字符串,但结果应该是2
因为第三个是在最后。这是我到目前为止有:
public static void main(String[] args) {
int count =0;
String text = "sentence";
Pattern pat = Pattern.compile("[e]+");
Matcher m = pat.matcher(text);
while (m.find()) {
count++;
}
System.out.println(count);
}
你可以在应用正则表达式没有最后一个字符的子字符串 –
你必须使用正则表达式吗?这项任务接近于更适合简单的循环。 –
你能解释一下如何使用。我需要计算e的中间值,但不应该把这个e作为它的最后一个字符。 –