Unfortunately you wont be able to remove this empty first element using only split
. You should probably remove first elements from your string that match your delimiters and split after it. Also your regex seems to be incorrect because
- by adding
{2,}
inside[...]
you are in making{
2
,
and}
characters delimiters, - you don't need to escape rest of your delimiters (note that you don't have to escape
-
only because it is at end of character class[]
so he cant be used as range operator).
Try maybe this way
String regexDelimiters = "[\\s,\"():;.!?\\-]+";
String inputString = "\"Word\" some more text.";
String[] arrayOfWords = inputString.replaceAll(
"^" + regexDelimiters,"").split(regexDelimiters);
for (String s : arrayOfWords)
System.out.println("'" + s + "'");
output:
'Word'
'some'
'more'
'text'