Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3474

Re: Replace any multiple whitespaces with single white space

From Joel VanderWerf <joelvanderwerf@gmail.com>
Newsgroups comp.lang.ruby
Subject Re: Replace any multiple whitespaces with single white space
Date 2011-04-25 14:14 -0500
Organization Service de news de lacave.net
Message-ID <4DB5C81E.1080000@gmail.com> (permalink)
References <c7e16b34811c4729c009d79c4c2eef4d@ruby-forum.com> <4DB5C2CA.3070705@gmail.com> <BANLkTinBUd4p27WD_fAH6wKStipXq2khkQ@mail.gmail.com>

Show all headers | View raw


On 04/25/2011 12:02 PM, Josh Cheek wrote:
> On Mon, Apr 25, 2011 at 1:51 PM, Joel VanderWerf
> <joelvanderwerf@gmail.com>wrote:
>
>> On 04/25/2011 11:44 AM, Michelle Pace wrote:
>>
>>> Hello, I need to make the first string below into the second string.
>>> That is, only single white spaces are permitted.
>>>
>>> "1/4 WELDING    LEVER FRONT DRW 14844-C  MAT  WMA1CM-WLFRONT"
>>> into
>>> "1/4 WELDING LEVER FRONT DRW 14844-C MAT WMA1CM-WLFRONT"
>>>
>>>
>>>
>>> I want to use the sub! method. Why does the below code not work? Is my
>>> pattern incorrect?
>>>
>>> descrip = "1/4 WELDING    LEVER FRONT DRW 14844-C  MAT  WMA1CM-WLFRONT"
>>> descrip.sub!(/\s+/,' ')
>>> puts descrip
>>>
>>
>> sub! only affects the *first* match. You can substitute globally with gsub.
>> Also you might as well only match 2 or more spaces:
>>
>> descrip.gsub!(/\s\s+/,' ')
>>
>>
>
> I think the original regex is better, because leads to more consistent
> results:
>
> "hello\tworld    !".gsub(/\s\s+/,' ') # =>  "hello\tworld !"
> "hello\tworld    !".gsub(/\s+/,' ')   # =>  "hello world !"
>

Good point, but it depends on what you're trying to be consistent with. 
Maybe the goal is to squeeze space, but preserve tab layout for readability.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Replace any multiple whitespaces with single white space Michelle Pace <michelle@michellepace.com> - 2011-04-25 13:44 -0500
  Re: Replace any multiple whitespaces with single white space Joel VanderWerf <joelvanderwerf@gmail.com> - 2011-04-25 13:51 -0500
    Re: Replace any multiple whitespaces with single white space Josh Cheek <josh.cheek@gmail.com> - 2011-04-25 14:02 -0500
      Re: Replace any multiple whitespaces with single white space Joel VanderWerf <joelvanderwerf@gmail.com> - 2011-04-25 14:14 -0500
        Re: Replace any multiple whitespaces with single white space Gunther Diemant <g.diemant@gmx.net> - 2011-04-25 15:43 -0500
    Re: Replace any multiple whitespaces with single white space Brian Candler <b.candler@pobox.com> - 2011-04-26 07:03 -0500
      Re: Replace any multiple whitespaces with single white space Joel VanderWerf <joelvanderwerf@gmail.com> - 2011-04-26 11:27 -0500
      Re: Replace any multiple whitespaces with single white space Joel VanderWerf <joelvanderwerf@gmail.com> - 2011-04-26 11:31 -0500
  Re: Replace any multiple whitespaces with single white space John W Higgins <wishdev@gmail.com> - 2011-04-25 13:56 -0500
  Re: Replace any multiple whitespaces with single white space Alexander McMillan <alexandermcmillan@hotmail.com> - 2011-04-26 05:18 -0500

csiph-web