Java Regex get all numbers

Distractic :

I need to retrieve all numbers from a String, example :

"a: 1 | b=2 ; c=3.2 / d=4,2"

I want get this result :

  • 1
  • 2
  • 3.2
  • 4,2

So, i don't know how to say that in Regex on Java.

Actually, i have this :

(?<=\D)(?=\d)|(?<=\d)(?=\D)

He split letter and number (but the double value is not respected), and the result is :

  • 1
  • 2
  • 3
  • 2 (problem)
  • 4
  • 2 (problem)

Can you help me ?

Thanks :D

Arvind Kumar Avinash :

You can do it as follows:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        // Test
        String s = "a: 1 | b=2 ; c=3.2 / d=4,2";
        showNumbers(s);
    }

    static void showNumbers(String s) {
        Pattern regex = Pattern.compile("\\d[\\d,.]*");
        Matcher matcher = regex.matcher(s);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

Output:

1
2
3.2
4,2

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=20852&siteId=1