RegEx to remove non-digit and non-decimal point from string

silver :
String str = "$1,234.56"
str.replaceAll("[^//d]", "");

Desired Output:

1234.56

Bit lost on how to say keep the decimal point (.).

anubhava :

You may use:

str = str.replaceAll("[^\\d.]+", "");

[^\\d.] is negated character class that will match any character except digit or dot.

Added + to make this bit more efficient.

RegEx Demo

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=357359&siteId=1