【发布时间】:2019-11-16 02:32:29
【问题描述】:
我有一种在文本中搜索单词的方法,两者都通过参数插入。
public Integer findTheWord(String stringToCheck, String regexString) throws IOException {
int count = 0;
Pattern regexp = Pattern.compile("\\b" + regexString + "\\b");
Matcher matcher = regexp.matcher(stringToCheck);
while (matcher.find()) {
count++;
String matchString = matcher.group();
System.out.println(matchString);
}
System.out.println(count);
return count;
}
如何插入多个单词并返回每个单词的出现次数?
【问题讨论】:
-
来自docs - 逻辑运算符:
X|Y Either X or Y。在你的情况下 -[word1|word2|word3]... -
@dbl 这无济于事,因为一旦最左边的替代匹配项,交替将返回,而 OP 想要“返回每个匹配项的出现”。
-
点已被占用。我会留下评论,所以没有其他人会犯同样的错误......
-
我的意思是通过参数传递一个 ArrayList
并验证它们每个的匹配。 -
我认为如果你想要一个答案,你应该提供一些测试用例和预期的输出。尝试放足够多的东西:)
标签: java regex string search matcher