user user - 1 month ago 6
Java Question

Remove all characters that is not a number including period

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

Answer

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.

Comments