smya.dsh smya.dsh - 2 years ago 127
Java Question

how to extract numeric values from input string in java

How can I extract only the numeric values from the input string?

For example, the input string may be like this:

String str="abc d 1234567890pqr 54897";

I want the numeric values only i.e,
. All the alphabetic and special characters will be discarded.

tbl tbl
Answer Source
String str=" abc d 1234567890pqr 54897";
Pattern pattern = Pattern.compile("\\w+([0-9]+)\\w+([0-9]+)");
Matcher matcher = pattern.matcher(str);
for(int i = 0 ; i < matcher.groupCount(); i++) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download