From: Josh Cheek Date: 2011-04-26T04:02:11+09:00 Subject: Re: Replace any multiple whitespaces with single white space --485b396190fa775e3804a1c2d023 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Apr 25, 2011 at 1:51 PM, Joel VanderWerf 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 !" --485b396190fa775e3804a1c2d023--