Extract numbers from string

Discussion in 'Ruby' started by eggie5, Sep 25, 2007.

  1. eggie5

    eggie5 Guest

    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"
     
    eggie5, Sep 25, 2007
    #1
    1. Advertisements

  2. eggie5

    Phil Meier Guest

    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
     
    Phil Meier, Sep 25, 2007
    #2
    1. Advertisements

  3. eggie5

    Peña, Botp Guest

    From: Nicholas Clare [mailto:]=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
     
    Peña, Botp, Sep 25, 2007
    #3
  4. --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 --

    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--
     
    David A. Black, Sep 25, 2007
    #4
  5. eggie5

    Peña, Botp Guest

    From: David A. Black [mailto:]=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
     
    Peña, Botp, Sep 25, 2007
    #5
  6. 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.
     
    Jesús Gabriel y Galán, Sep 25, 2007
    #6
  7. eggie5

    Peña, Botp Guest

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

    wow. thanks for the bnchmark, Jesus.

    kind regards -botp
     
    Peña, Botp, Sep 25, 2007
    #7
  8. eggie5

    eggie5 Guest

    That's awesome, thanks.
     
    eggie5, Sep 25, 2007
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.