1

i have a string like this one:

288.999,224.004 283.665,258.338 313.332,293.005 312.332,336.671 270.999,389.338 371.998,412.338 

i try to parse the data to float values, but i want to sort it! The value before comma should by my x value and the value after comma should be the y value.

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
}

This code just creates a single group...How should i change my String pattern to get a second group with the y-Values...

favored result:

x:288.999
y:224.004 
x:283.665
y:258.338 
....
4

3 回答 3

8

保持简单,拆分就足够了:

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]);
}
于 2013-06-26T09:35:07.370 回答
2

您正在寻找的模式:

((?:\\d*\\.\\d+)|(?:\\d+\\.\\d*)) *, *((?:\\d*\\.\\d+)|(?:\\d+\\.\\d*))

此外, group(0) 会带来整个比赛,你宁愿寻找 group(1) 和 group(2)

于 2013-06-26T09:46:31.593 回答
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]);
    }
于 2013-06-26T09:55:05.913 回答