Java使用某些规则编辑文本
我必须使用某些规则编辑文本: 同一单词中的重复字母将减少为单个字母Java使用某些规则编辑文本,java,Java,我必须使用某些规则编辑文本: 同一单词中的重复字母将减少为单个字母 "Questions" instead of "QuestionssSsS" 单词之间的多个间隙将缩小为一个空格 "go to the cinema" instead of "go to the cinema" 从单词中分离出来的单个字母将连接到单词 "first ten person" instead of "firs t ten person" 例如: String s = "I am enouuug
"Questions" instead of "QuestionssSsS"
单词之间的多个间隙将缩小为一个空格
"go to the cinema" instead of "go to the cinema"
从单词中分离出来的单个字母将连接到单词
"first ten person" instead of "firs t ten person"
例如:
String s = "I am enouuugGh of an artis t to draw freely upon my imagination. ImaginatioOO n is more importan t than knowledge. KKkKkKnowledge is limited. Imagination encircles the wwWorl d.";
预期产出:
I am enough of an artist to draw freely upon my imagination. Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world.
请给出建议和建议
String s = "I am enouuugGh of an artis t to draw freely upon my imagination. ImaginatioOO n is more importan t than knowledge. KKkKkKnowledge is limited. Imagination encircles the wwWorl d.";
System.out.println(s);
System.out.println("========================================================");
s = s.replaceAll("\\s+"," ");
s = s.replaceAll("(?i)(\\w)\\1+","$1");
s = s.replaceAll("(\\w+) (\\w)(?=[ \\.\\?!,])","$1$2");
System.out.println(s);
输出:我是一个足够的艺术家,可以用我的想象力来画弗雷利。想象力比知识更重要。知识是有限的。想象力环绕着世界。
==> \\s+ Several whitespace characters
==> \\w means A word character, short for [a-zA-Z_0-9]
==> \\w+ will represent one or more characters of \\w class
we will also place it in group (\\w+) - this will be 2nd group
==> Pattern.CASE_INSENSITIVE flag is (?i)
==> $number is backreferrence
也许可以解释一下你做了什么,为什么?这是可行的,但如果你不熟悉正则表达式,就很难理解。我很乐意给出这个答案,但OP明确要求提供
建议和建议不起作用的解决方案,可能想自己做作业,但不知道从何处开始。这对我来说非常复杂:(@Pshemo你是对的。对不起。@user2742616代替这个正则表达式,你可以用它来循环这个问题。请尝试到目前为止。如果你不能,我会帮助你。家庭作业?你到目前为止尝试了什么?