我正在尝试构建一个正则表达式来“减少”Java 字符串中重复的连续子字符串。例如,对于以下输入:
The big black dog big black dog is a friendly friendly dog who lives nearby nearby.
我想得到以下输出:
The big black dog is a friendly dog who lives nearby.
这是我到目前为止的代码:
String input = "The big black dog big black dog is a friendly friendly dog who lives nearby nearby.";
Pattern dupPattern = Pattern.compile("((\\b\\w+\\b\\s)+)\\1+", Pattern.CASE_INSENSITIVE);
Matcher matcher = dupPattern.matcher(input);
while (matcher.find()) {
input = input.replace(matcher.group(), matcher.group(1));
}
对于除句子末尾之外的所有重复子字符串来说,这都很好:
The big black dog is a friendly dog who lives nearby nearby.
我知道我的正则表达式需要在子字符串中的每个单词后面有一个空格,这意味着它不会捕获带有句点而不是空格的情况。我似乎无法找到解决方法,我尝试使用捕获组并更改正则表达式以查找空格或句点而不仅仅是空格,但此解决方案仅在存在以下情况时才有效子字符串的每个重复部分之后的句点(“nearby.nearby.”)。
有人能指出我正确的方向吗?理想情况下,此方法的输入将是短段落,而不仅仅是单行话。
您可以使用
input.replaceAll("([ \\w]+)\\1", "$1");
See 现场演示: https://ideone.com/l2mdfG
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String input = "The big black dog big black dog is a friendly friendly dog who lives nearby nearby.";
Pattern dupPattern = Pattern.compile("([ \\w]+)\\1", Pattern.CASE_INSENSITIVE);
Matcher matcher = dupPattern.matcher(input);
while (matcher.find()) {
input = input.replaceAll("([ \\w]+)\\1", "$1");
}
System.out.println(input);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)