我有以下字符串
:
0 0.123 1.43 4 hello
我需要使用正则表达式解析它并提取所有数字 float 和整数。 我需要这样的东西:
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));
我该怎么做?
最佳答案
您可以使用以下正则表达式
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
}
关于java - 如何将字符串解析为组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59140858/