-1

我有一些包含多个标点符号的文本文件,所以我需要将它们减少为单个标点符号。

这是一些示例文本:

They are working in London..... he is a Java developer !!!!! they are playing------ She is working_______

这是所需的输出:

They are working in London.he is a Java developer !they are playing- She is working_

我需要一些有关 Java 正则表达式的帮助。

谢谢

4

3 回答 3

2

使用反向引用 ( \1+) 匹配重复的字符。

尝试以下操作:

String text = "They are working in London..... he is a Java developer !!!!! they are playing------ ---- ---- She is working_______";
String replaced = text.replaceAll("(?:([-.!_])\\1+\\s*)+", "$1");
System.out.println(replaced);

印刷

They are working in London.he is a Java developer !they are playing-She is working_
于 2013-08-19T05:54:21.180 回答
0

你可以试试这个

   String str = "They are working in London..... he is a Java developer !!!!! they are playing-----She is working_______";
   String newStr = str.replaceAll("([|\\-|\\.|\\!|\\_])\\1+", "$1");
   System.out.println(newStr);

现场演示

输出

They are working in London. he is a Java developer ! they are playing-She is working_
于 2013-08-19T06:06:25.083 回答
-1

尝试这样的事情:

/([.;,?!-_]){2,}/$1/

于 2013-08-19T05:57:53.720 回答