Regex that fail when there excessive whitespaces between words

71 views Asked by At

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.

4

There are 4 answers

1
Jonas P. On BEST ANSWER

After reading this answer https://stackoverflow.com/a/1240365/2136936

I came up with the following regex, which is exactly what I wanted:

^[\\S](?!.*\\s{2,}).*[\\S]$

One thing I don't understand though, is why it doesn't work this way: ^[\\S].*(?!\\s{2,}).*[\\S]$

0
anubhava On

For failure case just check for:

\s{2,}

i.e. a whitespace 2 or more times. If there is a match then fail the verification.

0
guy_sensei On

This should check before and after and more than two spaces

[^\s]([ ]{2,})[^\s]
0
mihi On

You can use the following regex for it (assuming you do not want to match the empty string):

"^\\S++(\\s\\S++)*+$"

First a nonzero amount of non-whitespaces, and then multiple (possible zero) repetitions of a single whitespace followed by multiple (non-zero) non-whitespaces.

Instead of the non-backtracking ++ and *+ operators you can also use (with same result) the normal + and * operators; note that the performance of the non-backtracking operators will be a lot better in case the string is long (several kilobytes).