I get a simple string from which I want to extract some values. The values are separated by whitespace characters as follows:
abc 0.00 11.00 0.00 4.50 0.00 124.00 27.56 0.01 1.44 0.89 0.40
I want to get those values: abc, 0.00, 11.00,...
I tried this:
String line = "abc 0.00 11.00 0.00 4.50 0.00 124.00 27.56 0.01 1.44 0.89 0.40";
String regex = "^([\\w\\.]*)\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\$";
Pattern ptrn = Pattern.compile(regex);
Matcher matcher = ptrn.matcher(line);
if(matcher.find())
{
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
System.out.println(matcher.group(4));
System.out.println(matcher.group(5));
System.out.println(matcher.group(6));
System.out.println(matcher.group(7));
System.out.println(matcher.group(8));
System.out.println(matcher.group(9));
System.out.println(matcher.group(10));
System.out.println(matcher.group(11));
System.out.println(matcher.group(12));
}
I am getting following output:
abc
0
0
0
0
0
0
6
1
4
9
0
What I am doing wrong?
^([\\w\\.]*)\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\\s+([\\w\\.])*\$just to split this string into it's parts?String[] arr = line.split("\\s+");