aspose file tools*
The moose likes Java in General and the fly likes Splitting a String into an array using split() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Splitting a String into an array using split()" Watch "Splitting a String into an array using split()" New topic
Author

Splitting a String into an array using split()

Alex Lai
Greenhorn

Joined: Mar 21, 2009
Posts: 9
Hi,

I'm trying to split up a String using the split() method and place the substrings into an array for formulae interpretation. To elaborate more, I take Strings in the form like: "A2 + 1+B2", then split each and every character as substrings and stick them all in an array and interpret them one by one. So I would be ending up with:
stringArray[0] = "A2";
stringArray[1] = "+";
stringArray[2] = "1";
stringArray[3] = "+";
stringArray[4] = "B2";

Where stringArray is an array of Strings.

This is my plan:
First I check if the string is in of the form above, then I go and split them up. This is my (working) regular expression to check if it is of the form above:
"[A-Z]*[\\d]+[\\s]*[\\Q+-*/\\E]{1}[\\s]*[A-Z]*[\\d]+([\\s]*[\\Q+-*/\\E]{1}[\\s]*[A-Z]*[\\d]+)*[\\s]*"

Then, I split them up and place them in an array. The thing is, the String may or may not contain between the characters.
This is what I've tried so far in splitting up the String:

Would I need to add the logical OR operators in the regular expression?
Or would there be a better solution?

Help would be greatly appreciated.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


Since you have a better defintion of the tokens than the delimiter -- it may be better to have a regex for the tokens and use the find() method instead.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Alex Lai
Greenhorn

Joined: Mar 21, 2009
Posts: 9
So I use the find() method to see if a String contains a token that matches a regular expression, if it does, then I use the start() and end() methods to locate the token, extract it from the String and stick it into an index of an array? Then interpet each token accordingly?

Alex.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Alex Lai wrote:...
This is what I've tried so far in splitting up the String:

...


That's an extremely verbose regex:
- you're adding shorthand character classes inside a character class (\\d -> [\\d] and \\s -> [\\s]). It will do the job, but it is not necessary;
- {1} is never needed: in your regex, a character, or character class will always match a single character, no need for the extra {1};
- you're wrapping \\Q and \\E around characters which you have placed in a character class, but inside a character class, these characters (like + and *) don't have a special meaning.

Having said that, your regex:



is exactly the same as:



Note that the hyphen, '-', looses it's special range-meaning inside a character class when placed at the start or end of the class.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Alex Lai wrote:So I use the find() method to see if a String contains a token that matches a regular expression, if it does, then I use the start() and end() methods to locate the token, extract it from the String and stick it into an index of an array? Then interpet each token accordingly?

Alex.


You don't need to use start() and end(), you simply group() them, but yeah, you got the gist of it.

Here's a small demo:

Alex Lai
Greenhorn

Joined: Mar 21, 2009
Posts: 9
I just cannot thank you enough now! Thank you

I've just studied the code and added a few things to shape it more to my intentions, such as adding a few possessive quantifiers to a some characters.
Now I can take the ArrayList and process each token individually and return a result from a given formula.

Again, I must thank you all very much for the time in correcting my redundant errors and 'hinting' me an a solution.

Alex.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Alex Lai wrote:I just cannot thank you enough now! Thank you

I've just studied the code and added a few things to shape it more to my intentions, such as adding a few possessive quantifiers to a some characters.
Now I can take the ArrayList and process each token individually and return a result from a given formula.

Again, I must thank you all very much for the time in correcting my redundant errors and 'hinting' me an a solution.

Alex.


You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Splitting a String into an array using split()