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

Parsing out text in a predictable pattern

 
Terry Chambers
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a string of text:

word 1 1 4.0 text 1 2 3.5 dog 3 4 1.0 walker 3 2 4.0

The pattern is:

|<word> <number> <number> <number.number>| |<word> <number> <number> <number.number>| |<word> <number> <number> <number.number>|

I want to be able to take this string of text and break it down into tokens, just like the String Tokenizer does. However, I can't seem to figure out how to do it since I can't break it down by white space (although, I guess I could tokenize and then count to see if it is the 4th item to determine if I should do something different.

What is the most efficient way to handle this?

Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.Scanner. You can use the next() and nextInt() methods. The . can be found using next(String pattern) using the right pattern.

You could also use java.util.regex.Pattern in combination with java.util.regex.Matcher. The regular expression is quite simply, and using capturing groups you will get a simple loop where you can retrieve all 5 values in the body.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic