How to search for occurrences of more than one space between words in a line

Sam picture Sam · Sep 21, 2010 · Viewed 178.6k times · Source

How to search for occurrences of more than one space between words in a line

1. this is a line containing  2 spaces
2. this is a line containing   3 spaces
3. this is a line containing multiple spaces first  second   three   four

All the above are valid matches for this regex. What regex should I use?

Answer

AlexanderMP picture AlexanderMP · Sep 21, 2010
[ ]{2,}

SPACE (2 or more)

You could also check that before and after those spaces words follow. (not other whitespace like tabs or new lines)

\w[ ]{2,}\w

the same, but you can also pick (capture) only the spaces for tasks like replacement

\w([ ]{2,})\w

or see that before and after spaces there is anything, not only word characters (except whitespace)

[^\s]([ ]{2,})[^\s]