For example
例如
String text = "sentence"; // (number of e's = 2)
There are three e's in that string, but the result should be 2
because the third one is at the very end. This is what I have so far:
这个字符串里有三个e,但结果应该是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);
}
2 个解决方案
#1
3
Replace +
which exists after e
with negative lookahead. e+
matches one or more e
's , so regex engine should consider eee
as single match. And a negative lookahead after e
, ie e(?!$)
helps to find all the e
's but not the one which exists at the end of a line.
将e后面的+替换为- lookahead。e+匹配一个或多个e,因此regex引擎应该将eee视为单个匹配。在e(?!$)后面的一个负号可以帮助找到所有的e,而不是在一行末尾存在的e。
int count = 0;
String text = "sentence";
Pattern pat = Pattern.compile("e(?!$)");
Matcher m = pat.matcher(text);
while (m.find()) {
count++;
}
System.out.println(count);
#2
0
Matcher
methods can tell you the start and end index of the match. If end (next character after) matches the length of the string then it's the last character. E.g.
Matcher方法可以告诉您匹配的开始和结束索引。如果结束(后面的下一个字符)匹配字符串的长度,那么它就是最后一个字符。如。
int count =0;
String text = "sentence";
Pattern pat = Pattern.compile("e");
Matcher m = pat.matcher(text);
while (m.find() && m.end() != text.length()) {
count++;
}
System.out.println(count);
If you would like to exclude from counting last letter of a word instead of last word of the sentence, you can check whether the end character is alpha :
如果你想排除一个单词的最后一个字母而不是句子的最后一个单词,你可以检查结束字符是否为alpha:
int count =0;
String text = "sentence";
Pattern pat = Pattern.compile("e");
Matcher m = pat.matcher(text);
while (m.find() &&
m.end() != text.length() &&
Character.isAlphabetic(text.charAt(m.end()))) {
count++;
}
System.out.println(count);
#1
3
Replace +
which exists after e
with negative lookahead. e+
matches one or more e
's , so regex engine should consider eee
as single match. And a negative lookahead after e
, ie e(?!$)
helps to find all the e
's but not the one which exists at the end of a line.
将e后面的+替换为- lookahead。e+匹配一个或多个e,因此regex引擎应该将eee视为单个匹配。在e(?!$)后面的一个负号可以帮助找到所有的e,而不是在一行末尾存在的e。
int count = 0;
String text = "sentence";
Pattern pat = Pattern.compile("e(?!$)");
Matcher m = pat.matcher(text);
while (m.find()) {
count++;
}
System.out.println(count);
#2
0
Matcher
methods can tell you the start and end index of the match. If end (next character after) matches the length of the string then it's the last character. E.g.
Matcher方法可以告诉您匹配的开始和结束索引。如果结束(后面的下一个字符)匹配字符串的长度,那么它就是最后一个字符。如。
int count =0;
String text = "sentence";
Pattern pat = Pattern.compile("e");
Matcher m = pat.matcher(text);
while (m.find() && m.end() != text.length()) {
count++;
}
System.out.println(count);
If you would like to exclude from counting last letter of a word instead of last word of the sentence, you can check whether the end character is alpha :
如果你想排除一个单词的最后一个字母而不是句子的最后一个单词,你可以检查结束字符是否为alpha:
int count =0;
String text = "sentence";
Pattern pat = Pattern.compile("e");
Matcher m = pat.matcher(text);
while (m.find() &&
m.end() != text.length() &&
Character.isAlphabetic(text.charAt(m.end()))) {
count++;
}
System.out.println(count);