I have a question similar to How to split a string, but also keep the delimiters?. How would I split a String using a regex, keeping some types of delimiters, but not others? Specifically, I want to keep the non-whitespace delimiters, but not the whitespace delimiters.
To make this concrete:
"a;b c" | ["a", ";", "b", "c"]
"a; ; bb c ;d" | ["a", ";", ";", "bb", "c", ";", "d"]
Can this be done cleanly with a regex, and if so how?
Right now I'm working around this by splitting on the character to keep, and then again on the other one. I can stick with this approach if the regex cannot do so, or cannot do so cleanly:
Arrays.stream(input.split("((?<=;)|(?=;))"))
.flatMap(s -> Arrays.stream(s.split("\\s+")))
.filter(s -> !s.isEmpty())
.toArray(String[]::new); // In practice, I would generally use .collect(Collectors.toList()) instead