I have this string:
this is a test
Depending on the regex flavor, you can use two approaches.
If atomic groups/possessive quantifiers are not supported, use a lookahead solution like this:
See the regex demo
The main point is that we only match a whitespace that is not followed with 0+ other whitespace symbols followed with an end of string (the check if performed with the
See another demo. An equivalent expression with an atomic groups is
Here, we check for the end of string position ONLY after grabbing all whitespaces without backtracking into the
\s++ pattern (so, if after the last space there is an end of string, the whole match is failed).
However, this pattern is costly in terms of performance.