13

I am currently using this regex replace statement:

currentLine = Regex.Replace(currentLine, " {1,} \t \n", @" ");

It doesn't seem to be working.

I need a regular expression, that replaces white space(s), new line characters and/or tabs with a single white space.

Are there any other space characters, that I should have in mind ?

Ani Menon
  • 25,420
  • 16
  • 92
  • 119
Evaldas B
  • 2,234
  • 3
  • 15
  • 24
  • http://stackoverflow.com/questions/206717/how-do-i-replace-multiple-spaces-with-a-single-space-in-c – Sybren Oct 30 '14 at 18:48
  • your regex will lookfor one or more space followed by tab then space then a new line. You should use or (|) operator if you want to match one of them. Something like "\s+|\t+|\n+" – Learner Oct 30 '14 at 18:51

2 Answers2

24

For all whitespace use:

\s+

for specific chars you can use:

[ \t\n]+

Other space characters are \r and \f

codenheim
  • 19,781
  • 1
  • 55
  • 78
4
currentLine = Regex.Replace(currentLine, @"\s+", " ");

+ is shorthand for 1 or more and \s is "whitespace".

Guvante
  • 18,245
  • 1
  • 31
  • 64