Delete all characters that are not a number, including a period

advertisements

I am not understanding of the replaceAll method works. More specifcally the first argument takes a string regex. I would like to remove all characters that is not a number, including periods.

my implementation.

    userId = inputRow.next().replaceAll("[\\.^\\d.]", "");

Sample output:

"","",""
"","",""
"","",""
"","BBLDX",""
"","N",""
"","",""
"","",""
"","",""
"","",""
"","",""
"","",""
"","",""
"","",""
"","",""

Its removing everything besides alphabets


You can do it this way:

userId = inputRow.next().replaceAll("[^\\d]", "");

[^\\d] will match all the character sequence which is not a number and replace it with empty string.