Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I useDelimiter \t(ab) with my Scanner, but it splits around spaces

 
Simone Cittadini
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to parse files like this :

0093Afghanistan0,1775
009370Afghanistan Mobile0,1789
009379 [tab] Afghanistan [space] Mobile [space] Roshan [tab] 0,1789

so I use code like this :



but I get as output :

0093
Afghanistan
0,1775
009370
Afghanistan
Mobile
0,1789
009379
Afghanistan
Mobile
Roshan
0,1789

instead of what I want :

0093
Afghanistan
0,1775
009370
Afghanistan Mobile
0,1789
009379
Afghanistan Mobile Roshan
0,1789

Where I do wrong ?
 
Nick Meverden
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String test = "this" + '\u0009' + "is a" + '\u0009' + "test";
java.util.regex.Matcher m = java.util.regex.Pattern.compile("^(.+)\t(.+)\t(.+)$").matcher(test);
boolean matches = m.matches();
if (matches) {
System.out.println("1 " + m.group(1));
System.out.println("2 " + m.group(2));
System.out.println("3 " + m.group(3));
}
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Simone Cittadini
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I was not understanding how Scanner works, thought the default was to find carriage returns in a text file. Looked what was happening with the debugger and came out with this :

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic