How to replace all punctuation except double quotes using RegExp

I am trying to do some string cleaning.

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

Below the trimPunctuation () function works fine when removing all punctuation from a string.

Does anyone know a way to remove all punctuation, but 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(); } 

More information about the Unicode class Punctuation can be found here . But that did not help me.

+2
source share
1 answer

You can use negative viewing .

 (?!")\\p{punct} 

Rubular demo

Java example :

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

+9
source

Source: https://habr.com/ru/post/948963/


All Articles