[英]User Regex in java to find phrases
嗨,我正在尝试使用正则表达式发现字符串中的短语,我有以下代码:它似乎找不到所有两个单词短语。
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));
}
}
我为此而苦苦挣扎,为什么你好你好组不被淘汰? 任何帮助表示感谢。 如何更改模式以查找所有短语? 理查德
matcher.find()
始终从上一个匹配的位置进行搜索。 第一个调用发现:
test Test hello hello hello test test
所以剩下要做的就是
hello hello
在末尾。 最后一个hello hello
与您的模式不匹配(因为它只有两个单词,并且您的模式至少需要四个单词:它将2个单词作为组2
和3
抓取,然后稍后找到\\2\\W\\3
),所以它没有得到输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.