我有以下string
:
0 0.123 1.43 4 hello
,我需要使用regexp解析它并提取所有数字float和integer。 我需要这样的东西:
Matcher matcher = Pattern.compile("(\d+([\.\,]\d+)?)+").matcher("0 0.123 1.43 4 hello");
int d = Integer.parseInt(matcher.group(0));
int d1 = Integer.parseInt(matcher.group(1));
int d2 = Integer.parseInt(matcher.group(2));
我该怎么办?
答案 0 :(得分:1)
您可以使用以下正则表达式
String regex = "\\d+(?:[.,]\\d+)?";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("0 0.123 1.43 4 hello");
while (matcher.find()) {
String number = matcher.group();
// number can be integer or float
}