"; test = test.replaceAll("[...">

Java replaceAll not working 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 he keep the dollar sign?

+4
source share
2 answers

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

If you meant #-'to interpret it as a three-character list, just avoid it:

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

or put it at the end of the list:

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

- .

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

:)

. java, , . [a-z], "" ?

Javadoc Pattern (Ctrl-F " " )

, , . , . , - , .

+7

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


All Articles