gpt4 book ai didi

java - 如何将字符串解析为组?

转载 作者:行者123 更新时间:2023-12-02 09:14:08 25 4
gpt4 key购买 nike

我有以下字符串:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com