Accepted answer
[ ]{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]


here is my solution


this will remove all the digits, commas and new lines but select the middle space such as data set of

  • 20171106,16632 escg0000018sb
  • 20171107,280 escg0000018sb
  • 20171106,70476 escg0000018sb


search for [ ]{2,}. this will find two or more adjacent spaces anywhere within the line. it will also match leading and trailing spaces as well as lines that consist entirely of spaces. if you don't want that, check out alexander's answer.

actually, you can leave out the brackets, they are just for clarity (otherwise the space character that is being repeated isn't that well visible :)).

the problem with \s{2,} is that it will also match newlines on windows files (where newlines are denoted by crlf or \r\n which is matched by \s{2}.

if you also want to find multiple tabs and spaces, use [ \t]{2,}.


this regex selects all spaces, you can use this and replace it with a single space


example in python

result = re.sub('\s+',' ', data))


simple solution:


this matches all occurrences of one or more whitespace characters. if you need to match the entire line, but only if it contains two or more consecutive whitespace characters:


if the whitespaces don't need to be consecutive:


Related Query

More Query from same tag