0

If i have this String:

String line = "This, is Stack; Overflow.";

And want to split it into the following array of strings:

String[] array = ...

so the array contains this output:

["This",",","is","Stack",";","Overflow","."]

What regex expression should i put into the split() method ?

1
  • My bad. Misread the question. Commented Mar 1, 2015 at 13:05

2 Answers 2

1

Just split your input according to the spaces or the boundaries which exists between a word character and a non-word character, vice-versa.

String s = "This, is Stack; Overflow.";
String parts[] = s.split("\\s|(?<=\\w)(?=\\W)");
System.out.println(Arrays.toString(parts));

\s matches any kind of whitespace character, \w matches a word character and \W matches a non-word character.

  • \s matches a space character.
  • (?<=\\w) Positive look-behind which asserts that the match must be preceded by a word character (a-z, A-Z, 0-9, _).
  • (?=\\W) Positive look-ahead which asserts that the match must be followed by a non-word character(any character other than the word character). So this (?<=\\w)(?=\\W) regex matches only the boundaries not a character.

  • Thus splitting the input according to the matches spaces and the boundaries will give you the desired output.

DEMO

OR

String s = "This, is Stack; Overflow.";
String parts[] = s.split("\\s|(?<=\\w)(?=\\W)|(?<=[^\\w\\s])(?=\\w)");
System.out.println(Arrays.toString(parts));

Output:

[This, ,, is, Stack, ;, Overflow, .]
Sign up to request clarification or add additional context in comments.

1 Comment

Could you just explain what the regex expression means ? Because there's a lot of weird syntax in there...
0

You can do that with this pattern:

\\s+|(?<=\\S)(?=[^\\w\\s])|(?<=[^\\w\\s])\\b

it trims whitespaces and deals with consecutive special characters, example:

With ;This, is Stack; ;; Overflow.

you obtain: [";", "This", ",", "is", "Stack", ";", ";", ";", "Overflow", "."]

But obviously, the more efficient way is to not use the split method but the find method with this pattern:

\\w+|[^\\w\\s]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.