[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Extract numbers from string

eggie5

9/25/2007 6:45:00 AM

Say I have a phone number "(555) 555-5555" and I want to extract the
numbers from it. What's the most succinct way to do this? E.g.:

old_num = "(555) 111-5555"
new_num = "5551115555"

old_num = "555-666-7777"
new_num = "5556667777"

8 Answers

Phil Meier

9/25/2007 6:55:00 AM

0

eggie5 schrieb:
> Say I have a phone number "(555) 555-5555" and I want to extract the
> numbers from it. What's the most succinct way to do this? E.g.:
>
> old_num = "(555) 111-5555"
> new_num = "5551115555"
>
> old_num = "555-666-7777"
> new_num = "5556667777"
>

Regexp and gsub is an answer.

old_num = "(555) 111-5555"
p old_num.gsub(/[^0-9]/, '')
==> "5551115555"

old_num = "555-666-7777"
p old_num.gsub(/[^0-9]/, '')
==> "5556667777"


BR Phil

Peña, Botp

9/25/2007 7:16:00 AM

0

From: Nicholas Clare [mailto:nickclare@gmail.com]
# new_num = old_num.scan(/\d/).join('')
lose this ^^

irb(main):003:0> old_num.scan(/\d/).join
=> "5551115555"

also,

irb(main):014:0> old_num.gsub(/[^\d]/,"")
=> "5551115555"
irb(main):015:0> old_num.tr("^0-9","")
=> "5551115555"
irb(main):018:0> old_num.delete("^0-9")
=> "5551115555"
irb(main):025:0> old_num.split(/[^\d]/).join
=> "5551115555"

i'd hope that ruby-doc can doc such related methods.. something like documenting "The Many-Ways" of ruby...

kind regards -botp

David A. Black

9/25/2007 7:21:00 AM

0

Peña, Botp

9/25/2007 7:32:00 AM

0

From: David A. Black [mailto:dblack@rubypal.com]

#Don't forget \D:
#
# old_num.gsub(/\D/, "")
#

arggh, why do i always forget the bigcases :(

anyway, fr now on if i think of reverse cases, think of \D-black :)

thank you and kind regards -botp



Jesús Gabriel y Galán

9/25/2007 8:31:00 AM

0

On 9/25/07, Peña, Botp <botp@delmonte-phil.com> wrote:
> From: David A. Black [mailto:dblack@rubypal.com]
>
> #Don't forget \D:
> #
> # old_num.gsub(/\D/, "")

I always wanted try the benchmark stuff, and this seemed like a good
opportunity :-).

cat remove_nondigits.rb && ruby remove_nondigits.rb
# remove_nondigits.rb
# 25 September 2007
#

require 'benchmark'

n = 100_000
old_num = "(555) 55-555-55"

Benchmark.bmbm do |x|
x.report("scan") do
n.times {old_num.scan(/\d/).join}
end
x.report("gsub") do
n.times {old_num.gsub(/[^\d]/,"")}
end
x.report("gsub2") do
n.times {old_num.gsub(/\D/, "")}
end
x.report("tr") do
n.times {old_num.tr("^0-9","")}
end
x.report("delete") do
n.times {old_num.delete("^0-9")}
end
x.report("split") do
n.times {old_num.split(/[^\d]/).join}
end
end
Rehearsal ------------------------------------------
scan 1.080000 0.000000 1.080000 ( 1.144654)
gsub 0.380000 0.010000 0.390000 ( 0.403057)
gsub2 0.390000 0.010000 0.400000 ( 0.416074)
tr 0.290000 0.020000 0.310000 ( 0.320764)
delete 0.270000 0.010000 0.280000 ( 0.294078)
split 0.700000 0.000000 0.700000 ( 0.714139)
--------------------------------- total: 3.160000sec

user system total real
scan 1.080000 0.020000 1.100000 ( 1.101550)
gsub 0.380000 0.010000 0.390000 ( 0.404968)
gsub2 0.380000 0.010000 0.390000 ( 0.409364)
tr 0.290000 0.020000 0.310000 ( 0.318723)
delete 0.280000 0.010000 0.290000 ( 0.294650)
split 0.700000 0.000000 0.700000 ( 0.713261)

So it seems that in terms of speed, delete wins, second place for tr,
and in the fight between [^\d] and \D the first one wins by a little
(very little) margin :-).

Cheers,

Jesus.

Peña, Botp

9/25/2007 8:45:00 AM

0

From: Jesús Gabriel y Galán [mailto:jgabrielygalan@gmail.com]
# I always wanted try the benchmark stuff, and this seemed
# like a good opportunity :-).

wow. thanks for the bnchmark, Jesus.

kind regards -botp


eggie5

9/25/2007 5:48:00 PM

0

On Sep 25, 1:31 am, "Jes?s Gabriel y Gal?n" <jgabrielyga...@gmail.com>
wrote:
> On 9/25/07, Pe?a, Botp <b...@delmonte-phil.com> wrote:
>
> > From: David A. Black [mailto:dbl...@rubypal.com]
>
> > #Don't forget \D:
> > #
> > # old_num.gsub(/\D/, "")
>
> I always wanted try the benchmark stuff, and this seemed like a good
> opportunity :-).
>
> cat remove_nondigits.rb && ruby remove_nondigits.rb
> # remove_nondigits.rb
> # 25 September 2007
> #
>
> require 'benchmark'
>
> n = 100_000
> old_num = "(555) 55-555-55"
>
> Benchmark.bmbm do |x|
> x.report("scan") do
> n.times {old_num.scan(/\d/).join}
> end
> x.report("gsub") do
> n.times {old_num.gsub(/[^\d]/,"")}
> end
> x.report("gsub2") do
> n.times {old_num.gsub(/\D/, "")}
> end
> x.report("tr") do
> n.times {old_num.tr("^0-9","")}
> end
> x.report("delete") do
> n.times {old_num.delete("^0-9")}
> end
> x.report("split") do
> n.times {old_num.split(/[^\d]/).join}
> end
> end
> Rehearsal ------------------------------------------
> scan 1.080000 0.000000 1.080000 ( 1.144654)
> gsub 0.380000 0.010000 0.390000 ( 0.403057)
> gsub2 0.390000 0.010000 0.400000 ( 0.416074)
> tr 0.290000 0.020000 0.310000 ( 0.320764)
> delete 0.270000 0.010000 0.280000 ( 0.294078)
> split 0.700000 0.000000 0.700000 ( 0.714139)
> --------------------------------- total: 3.160000sec
>
> user system total real
> scan 1.080000 0.020000 1.100000 ( 1.101550)
> gsub 0.380000 0.010000 0.390000 ( 0.404968)
> gsub2 0.380000 0.010000 0.390000 ( 0.409364)
> tr 0.290000 0.020000 0.310000 ( 0.318723)
> delete 0.280000 0.010000 0.290000 ( 0.294650)
> split 0.700000 0.000000 0.700000 ( 0.713261)
>
> So it seems that in terms of speed, delete wins, second place for tr,
> and in the fight between [^\d] and \D the first one wins by a little
> (very little) margin :-).
>
> Cheers,
>
> Jesus.

That's awesome, thanks.

Terry Poulin

9/26/2007 11:39:00 PM

0

Peña, Botp wrote:
> From: Jesús Gabriel y Galán [mailto:jgabrielygalan@gmail.com]
> # I always wanted try the benchmark stuff, and this seemed
> # like a good opportunity :-).
>
> wow. thanks for the bnchmark, Jesus.
>
> kind regards -botp
>
>
>

I find my self most often reaching for gsub! or tr but hey, that helps :-)

Thanks

TerryP.


--

Email and shopping with the feelgood factor!
55% of income to good causes. http://www.ip...