Search code examples
c#regexnewlinewhitespace

Remove extra whitespaces, but keep new lines using a regular expression in C#


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?


Solution

  • 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.