Why does replaceAll remove numbers from a string?

String foo = "a3#4#b";
String afterPunctutationRemoval = foo.replaceAll("[,.;:?!'-_\"/()\\{}]", "");
System.out.println(afterPunctutationRemoval);

he gives me "a ## b", can someone explain to me why?

Shouldn't the string be returned as it is?

+3
source share
3 answers

Your character class contains a range '.. _, which also matches numbers.

Put -at the beginning or end of the character class:

foo.replaceAll("[,.;:?!'_\"/()\\{}-]", "")

or remove it:

foo.replaceAll("[,.;:?!'\\-_\"/()\\{}]", "");
+11
source

'-_matches each character between 'and _.

+5
source

, - - \\-.

, \\{ , {, ? , - \\\\{

0

All Articles