java中正则表达式用于提取浮点类型数据 急

2025-01-05 15:01:41
推荐回答(4个)
回答1:

\\d+(\\.\\d+)?
这个不是分割符,我一会把程序写出来,你稍等一下。

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class T {

public static void main(String[] args) {
String[] strings = matcher("[1,2,22.4,34,44.8,55.2]");
for (int i = 0; i < strings.length; i++) {
System.out.println(strings[i]);
}
}

private static Pattern pattern = Pattern.compile("\\d+(\\.\\d+)?");

public static String[] matcher(String input) {
Matcher matcher = pattern.matcher(input);
List list = new ArrayList();
while (matcher.find()) {
list.add(matcher.group());
}
return list.toArray(new String[0]);
}

}

回答2:

String s = "[1,2,22.4,34,44.8,55.2]";
java.util.regex.Pattern p = java.util.regex.Pattern.compile("(\\d+\\.\\d+)");
java.util.regex.Matcher m = p.matcher(s);
while (m.find()) {
    //依次输出22.4 44.8 55.2
    System.out.println(m.group(1));
}

回答3:

先用replace把[]替换了,然后直接用,分割字符串。再转型就行了。

回答4:

String testString ="[1,2,22.4,34,44.8,55.2]" ;
Pattern pattern = Pattern.compile("[\\d\\.]+");
Matcher matcher = pattern.matcher(testString);
while(matcher.find())
{
System.out.println(matcher.group()); //打印
}
System.out.println("over");