Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正则表达式查找句子中的重复单词_Java_Regex - Fatal编程技术网

Java 正则表达式查找句子中的重复单词

Java 正则表达式查找句子中的重复单词,java,regex,Java,Regex,我试着写一个正则表达式来找出句子中重复的单词。 我试着用这个表达: \b(\w+)\b.*?\1 从“你好,你好吗?你还好吗?你好吗?”这句话中选择3个“你好”,2个“是”,2个“你”,我知道这显然是错误的,因为它考虑了整个词组,而不是一个特定的词 那么,你能纠正我的表达方式或提出你自己的解决方案吗? 我正在使用Matcher类在Matcher.find()的while循环中使用count变量来尝试找出给定单词的出现次数。请尝试以下模式:(?Regex不太适合这样的工作。Regex不会计算东西

我试着写一个正则表达式来找出句子中重复的单词。 我试着用这个表达:

\b(\w+)\b.*?\1
从“你好,你好吗?你还好吗?你好吗?”这句话中选择3个“你好”,2个“是”,2个“你”,我知道这显然是错误的,因为它考虑了整个词组,而不是一个特定的词
那么,你能纠正我的表达方式或提出你自己的解决方案吗?

我正在使用
Matcher
类在
Matcher.find()的while循环中使用count变量来尝试找出给定单词的出现次数。请尝试以下模式:
(?Regex不太适合这样的工作。Regex不会计算东西。你可以在Regex的帮助下完成这项工作,但如果不是不可能的话,仅使用Regex是非常困难的

以下是我的尝试:

String sentence = "Hello how in the Hello world are you ? are you okay? Hello";
String[] words = Pattern.compile("\\W+").split(sentence); // split the sentence into words

Map<String, Integer> list = Arrays.stream(words)
        .collect(Collectors.groupingBy(x -> x))
        .entrySet().stream()
        .filter(x -> x.getValue().size() != 1) // remove the words that are not repeated 
        .collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue().size()));
String-sense=“你好,你好吗?你还好吗?你好”;
String[]words=Pattern.compile(\\W+)。拆分(句子);//将句子拆分为单词
映射列表=数组.流(字)
.collect(收集器.groupingBy(x->x))
.entrySet().stream()
.filter(x->x.getValue().size()!=1)//删除不重复的单词
.collect(Collectors.toMap(x->x.getKey(),x->x.getValue().size());

简单地将每个空格拆分,然后将所有项目放入一个
多集
/
计数集
/
计数集
会更容易。thanx,这真的很有帮助