views:

12112

answers:

4

What regex pattern would need to pass to the java.lang.String.split() method to split a string with all whitespace characters (' ', '\t', '\n', etc.) as delimiters?

+4  A: 

"\\s+" should do the trick

VonC
+6  A: 

In most regex dialects there are a set of convenient character summaries you can use for this kind of thing - these are good ones to remember:

\w - Matches any word character.

\W - Matches any nonword character.

\s - Matches any white-space character.

\S - Matches anything but white-space characters.

\d - Matches any digit.

\D - Matches anything except digits.

A search for "Regex Cheatsheets" should reward you with a whole lot of useful summaries.

glenatron
+15  A: 

Something in the lines of

myString.split("\\s+");

this groups all whitespaces as a delimiter... so if i have the string "Hello[space][tab]World", this should yield the strings "Hello" and "World" and omit the empty space between the space and the tab.

As VonC pointed out, the backslash should be escaped, because Java would first try to escape the string to a special character, and send that to be parsed. What you want, is the literal "\s", which means, you need to pass "\\s". It can get a bit confusing.

The \\s is equivalent to [ \\t\\n\\x0B\\f\\r]

Henrik Paul
\\s rather than \s, beware of the editor of StackOverflow: it eats '\' char ;) Note: in the comments, one '\' is enough...
VonC
Thank you for that reminder. I was just coding from the hip :)
Henrik Paul
You're welcome. +1 on your answer, much more complete than mine ;)
VonC
A: 

Perhaps a gentle reminder to RTFM ... java is one of the best-documented languages on the web; learning to navigate online docs will save you oodles of time on questions like this.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

Good luck!


Update for those who downvoted:

I'd appreciate your comments as to why this isn't helpful. Is it because this answer requires the OP to put a bit of personal effort into solving his or her own problem, rather than simply spoon-feeding the answer? Consider:

  • Rather than providing a partial answer, the link is a one-click all-inclusive solution.
  • Not only is the original question answered, but the OP now has the tools to become self-sufficient.
  • The ability to find answers, quickly and independently, to a wide variety or problems is far more valuable than asking for help every step of the way -- which type of person would you hire?
Adam Liss
not that i've downvoted, but i believe that it's because you haven't actually given the answer? You're not actually contributing to the greater resource of SO, just pointing to a different reference. To avoid this, I'd recommend pasting in a short summary as well as the link.
nickf
@nickf: Thanks for your thoughtful response -- it's appreciated! I'm curious as to what the "hive" thinks about this, as some links are upvoted and others are downvoted ... is the perceived value random, or dependent on something real?
Adam Liss
@Adam: The acronym "RTFM", though certainly relevant at times, is rather rude due to the embedded curse word. That probably has a lot to do with the downvote. We're humans; we're affected by emotion.
Christian Mann
@Christian: I appreciate your reply and apologize if my "gentle reminder to Read The Friendly Manual" offended you; it wasn't my intent. Also intended to teach, but not offend: in English, unlike programming languages, a comma goes inside, not outside, the quotes.
Adam Liss
@Adam: I refuse to abide by English's ridiculous principles. Kidding. I knew that; my fingers just instinctively reached for the quote mark first. I understand that it wasn't meant to offend. I was just offering a possible explanation for the downvote.
Christian Mann