Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3469 > unrolled thread
| Started by | Michelle Pace <michelle@michellepace.com> |
|---|---|
| First post | 2011-04-25 13:44 -0500 |
| Last post | 2011-04-26 05:18 -0500 |
| Articles | 10 — 7 participants |
Back to article view | Back to comp.lang.ruby
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
| From | Michelle Pace <michelle@michellepace.com> |
|---|---|
| Date | 2011-04-25 13:44 -0500 |
| Subject | Replace any multiple whitespaces with single white space |
| Message-ID | <c7e16b34811c4729c009d79c4c2eef4d@ruby-forum.com> |
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/.
[toc] | [next] | [standalone]
| From | Joel VanderWerf <joelvanderwerf@gmail.com> |
|---|---|
| Date | 2011-04-25 13:51 -0500 |
| Message-ID | <4DB5C2CA.3070705@gmail.com> |
| In reply to | #3469 |
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+/,' ')
[toc] | [prev] | [next] | [standalone]
| From | Josh Cheek <josh.cheek@gmail.com> |
|---|---|
| Date | 2011-04-25 14:02 -0500 |
| Message-ID | <BANLkTinBUd4p27WD_fAH6wKStipXq2khkQ@mail.gmail.com> |
| In reply to | #3470 |
[Note: parts of this message were removed to make it a legal post.] 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 !"
[toc] | [prev] | [next] | [standalone]
| From | Joel VanderWerf <joelvanderwerf@gmail.com> |
|---|---|
| Date | 2011-04-25 14:14 -0500 |
| Message-ID | <4DB5C81E.1080000@gmail.com> |
| In reply to | #3472 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Gunther Diemant <g.diemant@gmx.net> |
|---|---|
| Date | 2011-04-25 15:43 -0500 |
| Message-ID | <BANLkTinx1LMYDHgG3MyixWX9yTW_EKzosA@mail.gmail.com> |
| In reply to | #3474 |
[Note: parts of this message were removed to make it a legal post.]
There is also the build-in method squeeze!, which does exacly this
str.squeeze!(" ")
[toc] | [prev] | [next] | [standalone]
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Date | 2011-04-26 07:03 -0500 |
| Message-ID | <a880d1c094116d3d1f420a8a19b64e88@ruby-forum.com> |
| In reply to | #3470 |
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/.
[toc] | [prev] | [next] | [standalone]
| From | Joel VanderWerf <joelvanderwerf@gmail.com> |
|---|---|
| Date | 2011-04-26 11:27 -0500 |
| Message-ID | <4DB6F210.9080203@gmail.com> |
| In reply to | #3513 |
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"
[toc] | [prev] | [next] | [standalone]
| From | Joel VanderWerf <joelvanderwerf@gmail.com> |
|---|---|
| Date | 2011-04-26 11:31 -0500 |
| Message-ID | <4DB6F34F.9060509@gmail.com> |
| In reply to | #3513 |
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" 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)
[toc] | [prev] | [next] | [standalone]
| From | John W Higgins <wishdev@gmail.com> |
|---|---|
| Date | 2011-04-25 13:56 -0500 |
| Message-ID | <BANLkTinypZtaB7-HdANh5Ds-UcjiCcT90g@mail.gmail.com> |
| In reply to | #3469 |
[Note: parts of this message were removed to make it a legal post.] Good Morning Michelle, On Mon, Apr 25, 2011 at 11:44 AM, Michelle Pace <michelle@michellepace.com>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 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
[toc] | [prev] | [next] | [standalone]
| From | Alexander McMillan <alexandermcmillan@hotmail.com> |
|---|---|
| Date | 2011-04-26 05:18 -0500 |
| Message-ID | <BAY152-w619DB036B52E2C369850B8A6990@phx.gbl> |
| In reply to | #3469 |
Try gsub for multiple characters - Try code below: descrip = "1/4 WELDING LEVER FRONT DRW 14844-C MAT WMA1CM-WLFRONT" puts descrip.gsub!(/\s+/,' ')
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web