Extract numbers from string

E

eggie5

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"
 
P

Phil Meier

eggie5 said:
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
 
P

Peña, Botp

From: Nicholas Clare [mailto:[email protected]]=20
# new_num =3D old_num.scan(/\d/).join('')
lose this ^^

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

also,

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

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

kind regards -botp
 
D

David A. Black

--1926193751-1659981201-1190704791=:4353
Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-1659981201-1190704791=:4353"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--1926193751-1659981201-1190704791=:4353
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

From: Nicholas Clare [mailto:[email protected]]
# new_num =3D old_num.scan(/\d/).join('')
lose this ^^

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

also,

irb(main):014:0> old_num.gsub(/[^\d]/,"")
=3D> "5551115555"

Don't forget \D:

old_num.gsub(/\D/, "")


David

--=20
Upcoming training from Ruby Power and Light, LLC:
* Intro to Ruby on Rails, Edison, NJ, October 23-26
* Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!
--1926193751-1659981201-1190704791=:4353--
--1926193751-1659981201-1190704791=:4353--
 
P

Peña, Botp

From: David A. Black [mailto:[email protected]]=20

#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
 
J

Jesús Gabriel y Galán

From: David A. Black [mailto:[email protected]]

#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 =3D 100_000
old_num =3D "(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.
 
P

Peña, Botp

From: Jes=FAs Gabriel y Gal=E1n [mailto:[email protected]]=20
# I always wanted try the benchmark stuff, and this seemed
# like a good opportunity :).

wow. thanks for the bnchmark, Jesus.

kind regards -botp
 
E

eggie5

From: David A. Black [mailto:[email protected]]
#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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top