I've always been bad with regular expressions syntax. I am converting text into Bezier curves, polygons, etc. Part of that is identifying points in the form (x,y)
where x and y are coordinates in a graph. I am extracting x and y as doubles and creating Point objects with those two double values. I'm having problems with the white space. I cannot assume that there will be white space and I cannot assume that there won't be white space. Here's my attempt to extract three points from a String with some random white space thrown in.
String str = "( 4.5 , 8.9 ) (76.4, 9) (67.3 , 0.3) ";
Scanner scan = new Scanner(str);
double x[] = new double[3];
double y[] = new double[3];
int i;
try
{
for(i = 0; i < 3; i++)
{
scan.next("\\(");
x[i] = scan.nextDouble();
scan.next(",");
y[i] = scan.nextDouble();
scan.next("\\)");
}
for(i = 0; i < 3; i++)
{
System.out.print("(" + x[i] + "," + y[i] + ")");
}
}
catch(Exception ex)
{
System.out.println("malformed");
}
It works fine for the first point. Note that every token has white space after it for the first point. Then it gets to the second opening paren, which is followed by a digit rather than whitespace, and throws an InputMismatchException.
I'm wondering if it has something to do with "lazy" versus "greedy" pattern searching. I want it to stop when it finds the first pattern (in this case, the opening paren token), not keep going and try to keep matching, to match as little as possible and stop, so I guess that would be "lazy" (Java calls it "reluctant"?) As mentioned, I'm not very good with Regular Expressions (that's an understatement). I've been playing around with sticking in question marks, etc., but haven't got it working.