Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将字符串解析为组?_Java_Regex - Fatal编程技术网

Java 如何将字符串解析为组?

Java 如何将字符串解析为组?,java,regex,Java,Regex,我有以下字符串: 0 0.123 1.43 4 hello 我需要使用regexp解析它,并提取所有浮点数和整数。 我需要这样的东西: 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

我有以下
字符串

0 0.123 1.43 4 hello
我需要使用regexp解析它,并提取所有浮点数和整数。 我需要这样的东西:

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。。。