如何使用RegExp替换除双引号以外的所有标点符号 [英] How to replace all the punctuation except double quotes using RegExp

查看:257
本文介绍了如何使用RegExp替换除双引号以外的所有标点符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行一些字符串清理.

I am trying to do some string cleanup.

我想删除字符串双引号外的所有标点符号.

I want to remove all the punctuation from the string except double quotes.

trimPunctuation()函数下方的功能非常适合删除字符串中的所有标点符号.

Below trimPunctuation() function works great in removing all the punctuation from the string.

除了双引号之外,没有人知道删除所有标点符号的方法.

Does anyone know a way to remove all the punctuation but the double quotes.

 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类的更多信息,请参见此处.但是,那对我没有帮助.

More info on Punctuation unicode class can be found here. But, that didn't help me.

推荐答案

您可以使用否定前瞻.

(?!")\\p{punct}

Rubular演示

Java示例:

String string = ".\"'";
System.out.println(string.replaceAll("(?!\")\\p{Punct}", ""));

这篇关于如何使用RegExp替换除双引号以外的所有标点符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆