Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple delimiters for .split in Java

Tags:

java

string

split

Right now my code only separates words by white space, but I also want to separate by '.' and "," too. Here is my current code:

for (String words : input.split("\\s+"))

For example, if the user entered "bread,milk,eggs" or "Um...awkss" It would consider that one word, and I want each word to be it's own word.

And while I'm here, I can't get

input.isAlpha() 

to work either.

like image 409
Bottlecaps Avatar asked Nov 14 '13 01:11

Bottlecaps


1 Answers

You can split using this regex

input.split("\\s+|.+|,+")

or simply:

input.split("[\\s.,]+")

Remember that a dot doesn't have to be escaped inside square brackets

like image 159
Dariusz Avatar answered Sep 27 '22 18:09

Dariusz