| Author |
Parse String
|
David O'Connel
Greenhorn
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
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
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.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
David:
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.
John.
|
 |
 |
|
|
subject: Parse String
|
|
|