Forum: Ruby on Rails Replace numbers and leave one single whitespace

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3275da7fdbd73cb4e7956fd0d29164de?d=identicon&s=25 Paul Bergstrom (palb)
on 2009-06-04 22:55
This is a string "test1more32and444more22".

How do I remove the numbers and replace them with a single whitespace?

I can do it but I'll get a whitespace for each digit and consequently
have three whitespaces when there is three digits.
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2009-06-04 23:02
(Received via mailing list)
2009/6/4 Pål Bergström <rails-mailing-list@andreas-s.net>:

> This is a string "test1more32and444more22".
>
> How do I remove the numbers and replace them with a single whitespace?

irb(main):005:0> "test1more32and444more22".gsub(/[0-9]+/, " ")
=> "test more and more "

HTH,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (conradwt)
on 2009-06-04 23:02
(Received via mailing list)
2009/6/4 Pål Bergström <rails-mailing-list@andreas-s.net>

>
> This is a string "test1more32and444more22".
>
> How do I remove the numbers and replace them with a single whitespace?
>
> I can do it but I'll get a whitespace for each digit and consequently
> have three whitespaces when there is three digits.


You can try the following and please remember that "Programming Ruby"
book
 is your
friend:

"test1more32and444more22".gsub( /[0-9]+/, ' ' )

Good luck,

-Conrad
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-06-04 23:11
(Received via mailing list)
On Thu, Jun 4, 2009 at 5:01 PM, Conrad Taylor <conradwt@gmail.com>
wrote:
> "test1more32and444more22".gsub( /[0-9]+/, ' ' )

or equivalently

"test1more32and444more22".gsub( /\d+/, ' ' )

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
3275da7fdbd73cb4e7956fd0d29164de?d=identicon&s=25 Paul Bergstrom (palb)
on 2009-06-05 00:12
Rick Denatale wrote:
> On Thu, Jun 4, 2009 at 5:01 PM, Conrad Taylor <conradwt@gmail.com>
> wrote:
>> "test1more32and444more22".gsub( /[0-9]+/, ' ' )
>
> or equivalently
>
> "test1more32and444more22".gsub( /\d+/, ' ' )
>
> --
> Rick DeNatale
>
> Blog: http://talklikeaduck.denhaven2.com/
> Twitter: http://twitter.com/RickDeNatale
> WWR: http://www.workingwithrails.com/person/9021-rick-denatale
> LinkedIn: http://www.linkedin.com/in/rickdenatale

Great. Thanks. :-)
This topic is locked and can not be replied to.