Replace numbers and leave one single whitespace


#1

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.


#2

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


#3

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


#4

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


#5

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. :slight_smile: