Suppose you have the following string:
white sand, tall waves, warm sun
It's easy to write a regular expression that will match the delimiters, which the Java String.split() method can use to give you an array containing the tokens "white sand", "tall waves" and "warm sun":
\s*,\s*
Now say you have this string:
white sand and tall waves and warm sun
Again, the regex to split the tokens is easy (ensuring you don't get the "and" inside the word "sand"):
\s+and\s+
Now, consider this string:
white sand, tall waves and warm sun
Can a regex be written that will match the delimiters correctly, allowing you to split the string into the same tokens as in the previous two cases? Alternatively, can a regex be written that will match the tokens themselves and omit the delimiters? (Any amount of white space on either side of a comma or the word "and" should be considered part of the delimiter.)
Edit: As has been pointed out in the comments, the correct answer should robustly handle delimiters at the beginning or end of the input string. The ideal answer should be able to take a string like ",white sand, tall waves and warm sun and " and provide these exact three tokens:
[ "white sand", "tall waves", "warm sun" ]
...without extra empty tokens or extra white space at the start or end of any token.
Edit: It's been pointed out that extra empty tokens are unavoidable with String.split(), so that's been removed as a criterion for the "perfect" regex.
Thanks everyone for your responses! I've tried to make sure I upvoted everyone who contributed a workable regex that wasn't essentially a duplicate. Dan's answer was the most robust (it even handles ",white sand, tall waves,and warm sun and " reasonably, with that odd comma placement after the word "waves"), so I've marked his as the accepted answer. The regex provided by nsayer was a close second.