10

I am using this regular expression,

Regex.Replace(value.Trim(), @"\s+", " ");

To trim and minimize extra spaces into one space.
The problem is that it also removes new lines from the text.

How can I fix the regex so that it will keep the new lines?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yaron Levi
  • 12,535
  • 16
  • 69
  • 118

2 Answers2

20

Exclude CRLF's [^\S\r\n]+ within the white space class. [^] is a negative class. \S is a negative class which equals not [ space, tab, ff, lf, cr ]

The thing about negative classes is it factored out of the group and applies to each member in the group separately. And like math, two negatives equal a positive.

Not not white space = [^\S] = [\s]

However, the negative condition applies to the next class item as well as the next...

So, now that white space is included, you can exclude particular white space items from the class. [^\S\r\n] means all white space except CR or LF.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
9

I haven't managed to test it in C# yet, but the following works on http://www.regexr.com/:

Regex.Replace(value.Trim(), @"[^\S\r\n]+", " ");

Credit goes to Match whitespace but not newlines

The Regex works by matching the negated character class of not-whitespace or return / newlines.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David E
  • 1,384
  • 9
  • 14