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.
Paul B. (Guest)
on 2009-06-05 00: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.
Hassan S. (Guest)
on 2009-06-05 01:02
(Received via mailing list)
2009/6/4 Pål Bergström <removed_email_address@domain.invalid>:

> 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 S. ------------------------ removed_email_address@domain.invalid
Conrad T. (Guest)
on 2009-06-05 01:02
(Received via mailing list)
2009/6/4 Pål Bergström <removed_email_address@domain.invalid>

>
> 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
Rick D. (Guest)
on 2009-06-05 01:11
(Received via mailing list)
On Thu, Jun 4, 2009 at 5:01 PM, Conrad T. <removed_email_address@domain.invalid>
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
Paul B. (Guest)
on 2009-06-05 02:12
Rick Denatale wrote:
> On Thu, Jun 4, 2009 at 5:01 PM, Conrad T. <removed_email_address@domain.invalid>
> 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.