I need help with Regex to extract numbers from a string that is missing decimals.
Input
2 4.0 6.0 7 8 4.60
Output
2 7 8
Edit: Another follow up question to this regarding Regex and Java. Lets say that I want to add stuff to what I found (what I really want is taking 2 4.0 7.5 and turn it into 2.0 4.0 7.5, meaning adding missing .0 to the ones without a decimal), how would I do that and still keep the same untouched things of my String?
For 2nd question: –
Here’s the solution for your 2nd requirement: –
For 1st question : –
You can use this Regex: –
This will match any sequence of
digitsfollowed and preceded by aspace. Also, it matches digits at the end of the strings with the use of^and$.Here’s the implementation : –
Output : –