aspose file tools
The moose likes Beginning Java and the fly likes Parsing out text in a predictable pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Parsing out text in a predictable pattern" Watch "Parsing out text in a predictable pattern" New topic
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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Parsing out text in a predictable pattern
 
Similar Threads
need help figuring this out.
AOOB exception in my CODE-PLEASE HELP!
Do while error, I'm going crazy
Numeric value convert to word format
Beer Song