用一次出现替换多个连续出现的字符

Mr_*_*Hmp 0 java string

我正在使用Java编写自然语言处理应用程序,我正在使用来自IMDB和亚马逊的数据.

我遇到了一个有类似单词的数据集partyyyyy.这些词对我的分类算法不利.所以,我想删除它们而party不是添加partyyyyyyy.

我怎样才能做到这一点?

Psh*_*emo 10

您可以使用正则表达式在其后至少两次查找具有相同字母的字母(因为我们不想删除像m中的正确字母comma)

String data="stoooooop partyyyyyy";
System.out.println(data.replaceAll("([a-zA-Z])\\1{2,}", "$1"));
//                                       |      |         |
//                                   group 1   match    replace with 
//                                             from     match from group 1
//                                             group 1
//                                             repeated 
//                                           twice or more
Run Code Online (Sandbox Code Playgroud)

输出:

stop party
Run Code Online (Sandbox Code Playgroud)