| Author |
Parsing out text in a predictable pattern
|
Terry Chambers
Greenhorn
Joined: May 11, 2009
Posts: 17
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Parsing out text in a predictable pattern
|
|
|