For Programmers: Free Programming Magazines  


Home > Archive > Java Help > December 2004 > Regular Expressions









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Regular Expressions
burchill

2004-12-20, 8:58 pm

I have been really struggling with regular expressions, can anyone tell
me how I would split up a string around the spaces and newlines.
Basically I want every word to be put into a string array but not the
spaces (which is what I am getting now).

--
Eps
Andrew McDonagh

2004-12-20, 8:58 pm

burchill wrote:
> I have been really struggling with regular expressions, can anyone tell
> me how I would split up a string around the spaces and newlines.
> Basically I want every word to be put into a string array but not the
> spaces (which is what I am getting now).
>
> --
> Eps


Sounds like you need to use the StringTokeniser class
klynn47@comcast.net

2004-12-20, 8:58 pm

You might also look at the split method

klynn47@comcast.net

2004-12-20, 8:58 pm

Also you can collapse white space to a single space first.

string.replaceAll("\\s+"," ").split(" ")
This will return a String array of the tokens

Ryan Stewart

2004-12-20, 8:58 pm

"Andrew McDonagh" <news@andrewcdonagh.f2s.com> wrote in message
news:cq7bop$fa1$1@news.freedom2surf.net...
> burchill wrote:
>
> Sounds like you need to use the StringTokeniser class

Not the best advice. From the docs:
"StringTokenizer is a legacy class that is retained for compatibility
reasons although its use is discouraged in new code. It is recommended that
anyone sing this functionality use the split method of String or the
java.util.regex package instead."

See my other post.


Ryan Stewart

2004-12-20, 8:58 pm

<klynn47@comcast.net> wrote in message
news:1103576113.209705.255230@f14g2000cwb.googlegroups.com...
> Also you can collapse white space to a single space first.
>
> string.replaceAll("\\s+"," ").split(" ")
> This will return a String array of the tokens
>

Why add the extra step?
string.split("\\s+");


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com