Q:

Extracting Floating Numbers From String Using Matcher and Pattern

Suppose
String str="123 abc 123.4 256";
Matcher m = Pattern.compile("\\d+").matcher(scan);
while (m.find()) 
{
    System.out.println(m.group(0));
}

By Using this code i am getting output as:
123
123
4
256

But in output i want it as 123 ,123.4,256. What pattern should i use to get desirable result or what are the other solution beside using Matcher And pattern.

A:

Your regex only looks for digits (the plus just means at least one). If you want decimals, you need to include the dot in the correct place
(\\d+\\.\\d+)

That will give you at least one digit, followed by a dot, followed by at least one digit.

