Regular expression for special characters in java

public static final String specialChars1= "\\W\\S";
String str2 = str1.replaceAll(specialChars1, "").replace(" ", "+");

public static final String specialChars2 = "`~!@#$%^&*()_+[]\\;\',./{}|:\"<>?";
String str2 = str1.replaceAll(specialChars2, "").replace(" ", "+");

No matter what str1, I want all characters other than letters and numbers to be removed and spaces to be replaced by a plus sign ( +).

My problem is that if I use specialChar1, it does not remove some characters, such as ;, ', ", and if I use specialChar2, it gives me an error:

java.util.regex.PatternSyntaxException: Syntax error U_REGEX_MISSING_CLOSE_BRACKET near index 32:

How can this be achieved? I searched, but could not find the perfect solution.

+3
source share
6 answers

This worked for me:

String result = str.replaceAll("[^\\dA-Za-z ]", "").replaceAll("\\s+", "+");

For this input line:

/ - +! @ # $% ^ & ()) ";: [] {} \ | wetyk 678dfgh

He gave this result:

+ wetyk + 678dfgh

+13
source

replaceAll expects regex:

public static final String specialChars2 = "[`~!@#$%^&*()_+[\\]\\\\;\',./{}|:\"<>?]";
+4
source

, "\W\S" , , , .

"[^\w\s]". : , , , . ( "[\W\S]", , , ).

, , . [], ( ) , , , .

:

String sequence = "qwe 123 :@~ ";

String withoutSpecialChars = sequence.replaceAll("[^\\w\\s]", "");

String spacesAsPluses = withoutSpecialChars.replaceAll("\\s", "+");

System.out.println("without special chars: '"+withoutSpecialChars+ '\'');
System.out.println("spaces as pluses: '"+spacesAsPluses+'\'');

:

without special chars: 'qwe 123  '
spaces as pluses: 'qwe+123++'

+, "\s+" ( ).

+2

:

[<#![CDATA[¢<(+|!$*);¬/¦,%_>?: # = "~ {@} \]]] # > ]`

"#"

0

, :

    text.replaceAll("\\p{Punct}+", "").replaceAll("\\s+", "+");

public static String cleanPunctuations(String text) {
    return text.replaceAll("\\p{Punct}+", "").replaceAll("\\s+", "+");
}

public static void test(String in){
    long t1 = System.currentTimeMillis();
    String out = cleanPunctuations(in);
    long t2 = System.currentTimeMillis();
    System.out.println("In=" + in + "\nOut="+ out + "\nTime=" + (t2 - t1)+ "ms");

}

public static void main(String[] args) {
    String s1 = "My text with 212354 digits spaces and \n newline \t tab " +
            "[`~!@#$%^&*()_+[\\\\]\\\\\\\\;\\',./{}|:\\\"<>?] special chars";
    test(s1);
    String s2 = "\"Sample Text=\"  with - minimal \t punctuation's";
    test(s2);
}

>

In=My text with 212354 digits spaces and 
 newline     tab [`~!@#$%^&*()_+[\\]\\\\;\',./{}|:\"?] special chars
Out=My+text+with+212354+digits+spaces+and+newline+tab+special+chars
Time=4ms
In="Sample Text="  with - minimal    punctuation's
Out=Sample+Text+with+minimal+punctuations
Time=0ms
0

@npinti

"\ w" "\ dA-Za-z"

:

String result = str.replaceAll("[^\\w ]", "").replaceAll("\\s+", "+");
0

All Articles