11

I'm using the following regex to capture a fixed width "description" field that is always 50 characters long:

(?.{50})

My problem is that the descriptions sometimes contain a lot of whitespace, e.g.

"FLUID        COMPRESSOR                          "

Can somebody provide a regex that:

  1. Trims all whitespace off the end
  2. Collapses any whitespace in between words to a single space
Chris Karcher
  • 2,252
  • 7
  • 24
  • 31

7 Answers7

15

Substitute two or more spaces for one space:

s/  +/ /g

Edit: for any white space (not just spaces) you can use \s if you're using a perl-compatible regex library, and the curly brace syntax for number of occurrences, e.g.

s/\s\s+/ /g

or

s/\s{2,}/ /g

Edit #2: forgot the /g global suffix, thanks JL

sk.
  • 6,336
  • 5
  • 38
  • 46
9
str = Regex.Replace(str, " +( |$)", "$1");
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
2

Perl-variants: 1) s/\s+$//; 2) s/\s+/ /g;

2

C#:

Only if you wanna trim all the white spaces - at the start, end and middle.

     string x = Regex.Replace(x, @"\s+", " ").Trim();
nawfal
  • 70,104
  • 56
  • 326
  • 368
1

Since compressing whitespace and trimming whitespace around the edges are conceptually different operations, I like doing it in two steps:

re.replace("s/\s+/ /g", str.strip())

Not the most efficient, but quite readable.

Casey Rodarmor
  • 14,878
  • 5
  • 30
  • 33
1

Is there a particular reason you are asking for a regular expression? They may not be the best tool for this task.

A replacement like

 s/[ \t]+/ /g

should compress the internal whitespace (actually, it will compress leading and trailing whitespace too, but it doesn't sound like that is a problem.), and

s/[ \t]+$/$/

will take care of the trailing whitespace. [I'm using sedish syntax here. You didn't say what flavor you prefer.]


Right off hand I don't see a way to do it in a single expression.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • I'm using this inside of a larger regular expression, from http://stackoverflow.com/questions/162727/read-fixed-width-record-from-text-file – Chris Karcher Oct 19 '08 at 19:54
0

/(^[\s\t]+|[\s\t]+([\s\t]|$))/g replace with $2 (beginning|middle/end)