Java Data Type How to - Take numbers from a string








Question

We would like to know how to take numbers from a string.

Answer

import java.util.regex.Matcher;
import java.util.regex.Pattern;
//w w  w .j av a2  s . c o  m
public class Main {
  public static void main(String[] args) {
    Pattern p = Pattern.compile("\\d+");
    Matcher m = p.matcher("(345+3)*/3asdf234234234/234234234/23asdfaasd1f2a3s4d54fadsf");
    while (m.find())
      System.out.println(m.group());
  }
}

The code above generates the following result.