我正在尝试进行一些字符串清理。
我想删除字符串中的所有标点符号除了双引号。
下面的trimPunctuation()函数非常适合从字符串中删除所有标点符号。
有谁知道如何删除除双引号之外的所有标点符号。
private String trimPunctuation( String string, boolean onlyOnce )
{
if ( onlyOnce )
{
string = string.replaceAll( "\\p{Punct}$", "" );
string = string.replaceAll( "^\\p{Punct}", "" );
}
else
{
string = string.replaceAll( "\\p{Punct}+$", "" );
string = string.replaceAll( "^\\p{Punct}+", "" );
}
return string.trim();
}
有关标点符号 unicode 类的更多信息可以找到 here 。但是,这对我没有帮助。
最佳答案
您可以使用 negative lookahead .
(?!")\\p{punct}
String string = ".\"'";
System.out.println(string.replaceAll("(?!\")\\p{Punct}", ""));
<小时/>
关于java - 如何使用 RegExp 替换除双引号之外的所有标点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9880941/