Extract numbers from a string in java

517 Views Asked by At

I want to extract numbers from a string like:

str="good_mor9ni13ng_23guys ";

The output should be an array [9,13,23]. How should I proceed?

1

There are 1 best solutions below

3
On BEST ANSWER

You could do it like this:

List<String> matches = new ArrayList<String>();
Matcher m = Pattern.compile("[0-9]+").matcher("good_mor9ni13ng_23guys");
while (m.find()) {
    matches.add(m.group());
}