This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Parse String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Parse String" Watch "Parse String" New topic

Parse String

David O'Connel

Joined: May 13, 2009
Posts: 1
I am taking a string input from the command line using a buffered reader and storing in a string.

This specific string is in the form USE <item> ON <item> where an item can be one or more words.

I am trying to store these two specified items in two separate strings, I have had trouble with both the string tokenizer and the .split() method.

When using the string tokenizer my loop gets far to confusing and ends up not working at all.

Where as with .split() I am having trouble coming up with the correct reg-ex to split it up as I want.

currently for the regular expression i am trying to use "\b[Oo][Nn]\b" to split around the ON or any upper/lower case variation.

However when I print out the array that is returned it just returns the string in its original form.

Any help would be greatly appreciated, thanks in advance.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11920

The first thing i would do would be to normalize your string (if you can). convert the whole thing to uppercase, then you don't have to worry about [oO] type nonsense.

there are many ways to approach this. One way might be to find the position of the substring " on ". you would then know that the relevant portion for the first item would be from the 4th position to the start of that substring. and since you know how long the " on " substring is, you can get whatever comes after it fairly easily.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
John de Michele

Joined: Mar 09, 2009
Posts: 600

Welcome to Java Ranch!

Java since Java regexes are Strings, you need to use double backslashes, like this:

Otherwise, Java will interpret the backslashes as escape sequences.

Another approach to your problem might be to split on one or more whitespace characters.

I agree. Here's the link:
subject: Parse String
It's not a secret anymore!