Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I filter a string on just numbers, dots and commas?

Tags:

java

regex

Okay, so ive got a long string, and I want to remove everything thats inside, except for decimal numbers, comma's and dots,

I have tried:

str = str.replace("[^0-9\\.\\,]","");

But this just ends up in nothing..

Can anyone help me?

Thanks!

like image 851
truy37 Avatar asked May 12 '11 14:05

truy37


2 Answers

You don't need to escape the characters in the character group. You should also be using replaceAll().

str = str.replaceAll("[^0-9.,]+","");
like image 148
jjnguy Avatar answered Oct 21 '22 20:10

jjnguy


Try str.replaceAll("[^0-9.,]+","");

like image 37
Martijn Avatar answered Oct 21 '22 22:10

Martijn