Replace any multiple whitespaces with single white space

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

Thank-you in advance,
Michelle

···

--
Posted via http://www.ruby-forum.com/.

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+/,' ')

···

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

Good Morning Michelle,

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 replaces the first instance of the pattern. You require gsub! to
accomplish your task.

You noticed no difference with your sub! call because the first instance of
your pattern is the single space between 1/4 and WELDING so in essence sub!
did nothing to your string because it replaced a single space with a single
space.

John

···

On Mon, Apr 25, 2011 at 11:44 AM, Michelle Pace <michelle@michellepace.com>wrote:

Try gsub for multiple characters - Try code below:

descrip = "1/4 WELDING LEVER FRONT DRW 14844-C MAT WMA1CM-WLFRONT"
puts descrip.gsub!(/\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 !"

···

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+/,' ')

Joel VanderWerf wrote in post #994935:

···

On 04/25/2011 11:44 AM, Michelle Pace wrote:

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+/,' ')

Those are not equivalent, because \s matches more than just ASCII 0x20.

d1 = "foo\tbar\tbaz"
d1.gsub(/\s+/,' ') # "foo bar baz"
d1.gsub(/\s\s+/,' ') # "foo\tbar\tbaz"

--
Posted via http://www.ruby-forum.com/\.

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.

···

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 !"

Joel VanderWerf wrote in post #994935:

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+/,' ')

Those are not equivalent, because \s matches more than just ASCII 0x20.

d1 = "foo\tbar\tbaz"
d1.gsub(/\s+/,' ') # "foo bar baz"
d1.gsub(/\s\s+/,' ') # "foo\tbar\tbaz"

···

On 04/26/2011 05:03 AM, Brian Candler wrote:

On 04/25/2011 11:44 AM, Michelle Pace wrote:

You're right. What I said in another post about preserving tabs isn't what the original sub! call was doing anyway.

(sorry for the empty reply previously)

···

On 04/26/2011 05:03 AM, Brian Candler wrote:

Joel VanderWerf wrote in post #994935:

On 04/25/2011 11:44 AM, Michelle Pace wrote:

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+/,' ')

Those are not equivalent, because \s matches more than just ASCII 0x20.

d1 = "foo\tbar\tbaz"
d1.gsub(/\s+/,' ') # "foo bar baz"
d1.gsub(/\s\s+/,' ') # "foo\tbar\tbaz"

There is also the build-in method squeeze!, which does exacly this

str.squeeze!(" ")