1

I have this code:

String[] parts = sentence.split("\\s");

and a sentence like: "this is a whitespace and I want to split it" (note there are 3 whitespaces after "whitespace")

I want to split it in a way, where only the last whitespace will be removed, keeping the original message intact. The output should be

"[this], [is], [a], [whitespace ], [and], [I], [want], [to], [split], [it]" (two whitespaces after the word "whitespace")

Can I do this with regex and if not, is there even a way?

I removed the + from \\s+ to only remove one whitespace

1
  • What should happen in situation when after last word there are also spaces like "foo bar " (there are two spaces after each word). What should be result here? Commented Nov 30, 2022 at 15:33

1 Answer 1

2

You can use

String[] parts = sentence.split("\\s(?=\\S)");

That will split with a whitespace char that is immediately followed with a non-whitespace char.

See the regex demo. Details:

  • \s - a whitespace char
  • (?=\S) - a positive lookahead that requires a non-whitespace char to appear immediately to the right of the current location.

To make it fully Unicode-aware in Java, add the (?U) (Pattern.UNICODE_CHARACTER_CLASS option equivalent) embedded flag option: .split("(?U)\\s(?=\\S)").

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! You're a real life saver! :)

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.