删除所有标点符号,但在Java中保留句号

时间:2015-01-28 09:18:43

标签: java regex string

String abc = "hey. how are you. doing tonight! when we meet?";
abc = abc.replaceAll("[\\p{Punct}&&[^'-]]+", " ");
System.out.println(abc);

以上代码的输出是:

hey  how are you doing tonight  when we meet 

但我想保留句号,以便我可以将其分成句子。 期望的输出:

hey. how are you.  doing tonight when we meet

请帮助正则表达式

2 个答案:

答案 0 :(得分:0)

以这种方式更改你的正则表达式:

abc = abc.replaceAll("[\\p{Punct}&&[^'-.]]+", " ");

因此.也不会被替换。

O / P:

hey. how are you. doing tonight  when we meet 

答案 1 :(得分:0)

(\\.)|[\\p{Punct}&&[^'-]]+

您可以使用$1\1替换此代码。这将首先捕获.,然后捕获剩余的标点字符,以便我们替换休息。