Java replaceAll fails with dollar sign in source string

Say I have the following code

String test = "$abc<>";
test = test.replaceAll("[^A-Za-z0-9./,#-' ]", "");

test is now "$abc".

Why does it keep the dollar sign?

Jon Skeet
people
quotationmark

Your list of characters to preserve includes #-', which is a range from Unicode U+0023 (the # symbol) to U+0027 (the ' symbol), including $ (U+0024).

If you meant #-' to be interpreted as a list of three characters, just escape it:

test = test.replaceAll("[^A-Za-z0-9./,#\\-' ]", "");

or put it at the end of the list:

test = test.replaceAll("[^A-Za-z0-9./,#' -]", "");

people

See more on this question at Stackoverflow