Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String replaceAll regex to remove everything except digits, dots and spaces

Tags:

java

string

regex

I've browsed a lot of regex topics but none of these lead me to success in my particular situation.

(Using java) I have some charsequence which I then convert to array and extract numbers to array of doubles.

asdsad 59 asdf .2 asdf 56 89 .a 2.4 wef 95 asdf.

then I want to use regex to remove the extra part and compose the following string

59 2 56 89 2.4 95

so then I could just use .split(" ") and put them all to an array of doubles.

Until this moment I used the following expression

[^0-9.\s]

but it leaves the extra dots and therefore not reliable. Now I'm trying something like

[^0-9.\s]|([^0-9]\.[^0-9])

but it's not working at all, I'm not really good with regex, so could you explain me why the last expression is not working and how to fix it.

like image 214
LPVOID Avatar asked Dec 04 '16 09:12

LPVOID


2 Answers

Have you tried string.replaceAll("[^\\d\\. ]","")?

You can see the results here: https://regex101.com/r/X6gLaY/2

String string = "asdsad 59 asdf 2 asdf 56 89 .a 2.4 wef 95 asdf.";
String regex = "[^\\d\\. ]| \\.|\\.$";
System.out.println(string.replaceAll(regex,""));

Java example: http://ideone.com/w4BWOZ

Outputs: 59 2 56 89 2.4 95

like image 176
Xaero Degreaz Avatar answered Oct 01 '22 15:10

Xaero Degreaz


I've played with regex for half a day until I come up with this.

Apparently it really matters what the order of the expression is. I assume it's because it iterates over each condition and always uses the data that's left after execution of previous condition, so I changed the regular expression to:

  1. exclude all dots followed by non-digit

  2. exclude all non-digits followed by dot

  3. exclude all left-over non-digits

[^0-9]\.|\.[^0-9]|[^0-9.\s]

Now it works like a charm. Hope it helps someone. :)

like image 28
LPVOID Avatar answered Oct 01 '22 15:10

LPVOID