我有一个像这样的字符串:
288.999,224.004 283.665,258.338 313.332,293.005 312.332,336.671 270.999,389.338 371.998,412.338
我尝试将数据解析为浮点值,但我想对其进行排序!逗号之前的值应该是我的x值,逗号之后的值应该是y值。
Pattern p = Pattern.compile("[0-9]+.[0-9]*");
Matcher m = p.matcher(pointString);
while(m.find())
{
System.out.print("x:"+m.group(0)); //x- Values
// System.out.print("y:"+m.group(1)); //y- Values
}
此代码只创建一个组...我应该如何更改我的String模式以获得具有y值的第二个组...
青睐的结果:
x:288.999
y:224.004
x:283.665
y:258.338
....
答案 0 :(得分:8)
保持简单,split就够了:
String input = "288.999,224.004 283.665,258.338 313.332,293.005 312.332,336.671 270.999,389.338 371.998,412.338";
String[] points = input.split(" ");
for (String point : points) {
String[] coordinates = point.split(",");
System.out.println("x:" + coordinates[0]);
System.out.println("y:" + coordinates[1]);
}
答案 1 :(得分:2)
您正在寻找的模式:
((?:\\d*\\.\\d+)|(?:\\d+\\.\\d*)) *, *((?:\\d*\\.\\d+)|(?:\\d+\\.\\d*))
另外,组(0)会带来整场比赛,你宁愿寻找组(1)和组(2)
答案 2 :(得分:0)
这将有效
String str = "288.999,224.004 283.665,258.338 313.332,293.005 312.332,336.671 270.999,389.338 371.998,412.338";
String[] points=str.split(" ");
String[] point=new String[2];
for(int i=0;i<points.length;i++){
point=points[i].split(",");
System.out.println("X-val: "+point[0]);
System.out.println("Y-val: "+point[1]);
}