I'm trying to make a regex in Java that does not match excessive whitespaces between words, and whitespaces at the beginning and end.
I have done this regex so far
^[\\S].*[\\S]$
That fail when there are whitespaces at the beginning and end of a line
But how about excessive whitespaces between words?
I want this line to fail:
"test test"
But not this:
"test test"
I tried this:
^[\\S].*(?![\\s]{2,}).*[\\S]$
But it didn't work.
After reading this answer https://stackoverflow.com/a/1240365/2136936
I came up with the following regex, which is exactly what I wanted:
One thing I don't understand though, is why it doesn't work this way:
^[\\S].*(?!\\s{2,}).*[\\S]$