Java Split string into words commas and full stops

java, regex, split

Solution

If commas and periods are always followed by whitespace or end-of-string, then you can write:

myString.split("(?=[,.])|\\s+");

If they're not and you want e.g. `a,b` to be split into three strings, then:

myString.split("(?<=[,.])|(?=[,.])|\\s+");

Problem

I have been using `myString.split("\\s+");` to get the each word. But now I want to split the commas and full stops aswell. For Example: Mama always said life was like a box of chocolates, you never know what you're gonna get. to: ``` {Mama, always, said, life, was, like, a, box, of, chocolates ,,, You, never, know, what, you're, gonna, get,.,} ``` How would one go about doing this?

Original source