您好我正在尝试使用正则表达式在字符串中发现短语我有以下代码:它似乎找不到所有这两个单词短语。
public static void main(String[] args) {
String inputText = "test and test Test hello hello hello test test hello hello ";
//Pattern pattern = Pattern.compile("((\\w{3,}?)\\W(\\w{3,}?)\\W).*\\2\\W\\3", Pattern.CASE_INSENSITIVE);
Pattern twoWordPrasePattern = Pattern.compile("(([a-zA-Z]{3,})\\W([a-zA-Z]{3,})\\W).*\\2\\W\\3", Pattern.CASE_INSENSITIVE);
Matcher matcher = twoWordPrasePattern.matcher(inputText);
while (matcher.find()) {
System.out.println(inputText.substring(matcher.start(), matcher.end()));
System.out.println(matcher.group(1));
}
}
我正在努力解决这个问题为什么hello hello group没有出来? 任何帮助表示感谢。如何更改模式以查找所有短语?理查德
答案 0 :(得分:3)
matcher.find()
始终会搜索上一场比赛中断的位置。第一个调用找到了这个:
test Test hello hello hello test test
所以留下来搜索的是
hello hello
最后。最终的hello hello
与您的模式不匹配(因为它只有两个单词,您的模式至少需要四个单词:它将两个单词作为组2
和3
抓取,然后