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.
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
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:
exclude all dots followed by non-digit
exclude all non-digits followed by dot
exclude all left-over non-digits
[^0-9]\.|\.[^0-9]|[^0-9.\s]
Now it works like a charm. Hope it helps someone. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With