16

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
  • 27,209
  • 16
  • 105
  • 126
Evaldas B
  • 2,464
  • 3
  • 17
  • 25
  • http://stackoverflow.com/questions/206717/how-do-i-replace-multiple-spaces-with-a-single-space-in-c – Sybren Oct 30 '14 at 18:48
  • 1
    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

27

For all whitespace use:

\s+

for specific chars you can use:

[ \t\n]+

Other space characters are \r and \f

codenheim
  • 20,467
  • 1
  • 59
  • 80
4
currentLine = Regex.Replace(currentLine, @"\s+", " ");

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

Guvante
  • 18,775
  • 1
  • 33
  • 64