Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to extract phone number and reformat?

Phone number data in various formats (I've chosen these because the data coming in is unreliable and not in expected formats):

+1 480-874-4666
404-581-4000
(805) 682-4726
978-851-7321, Ext 2606
413- 658-1100
(513) 287-7000,Toll Free (800) 733-2077
1 (813) 274-8130
212-363-3200,Media Relations: 212-668-2251.
323/221-2164

My Ruby code to extract all the digits, remove any leading 1's for the USA country code, then use the first 10 digits to create the "new" phone number in a format desired:

  nums = phone_number_string.scan(/[0-9]+/)
  if nums.size > 0
    all_nums = nums.join
    all_nums = all_nums[0..0] == "1" ? all_nums[1..-1] : all_nums
    if all_nums.size >= 10
      ten_nums = all_nums[0..9]
      final_phone = "#{ten_nums[0..2]}-#{ten_nums[3..5]}-#{ten_nums[6..9]}"
    else
      final_phone = ""
    end
    puts "#{final_phone}"
  else
    puts "No number to fix."
  end

The results are very good!

480-874-4666
404-581-4000
805-682-4726
978-851-7321
413-658-1100
513-287-7000
813-274-8130
212-363-3200
323-221-2164

But, I think there's a better way. Can you refactor this to be more efficient, more legible, or more useful?

like image 666
Kevin Elliott Avatar asked Jul 17 '09 10:07

Kevin Elliott


People also ask

How do you properly format a phone number?

To format phone numbers in the US, Canada, and other NANP (North American Numbering Plan) countries, enclose the area code in parentheses followed by a nonbreaking space, and then hyphenate the three-digit exchange code with the four-digit number.

Can you format phone numbers in Excel?

Excel provides a special number format that lets you format a number as a phone number. For example, you can format a 10-digit number, such as 5555551234, as (555) 555-1234. Select the cell or range of cells that you want to format.

Is there a phone number format in Google Sheets?

Format Phone Numbers in Google Sheets To format numbers as phone numbers with dashes in Google Sheets, first select the list of numbers you want to format. Then, in the Menu click Format > Number > More Formats > Custom number formats. 2. In the Custom number formats window, enter the format you want, then click Apply.


1 Answers

Here's a much simpler approach using only regexes and substitution:

def extract_phone_number(input)
  if input.gsub(/\D/, "").match(/^1?(\d{3})(\d{3})(\d{4})/)
    [$1, $2, $3].join("-")
  end
end

This strips all non-digits (\D), skips an optional leading one (^1?), then extracts the first remaining 10 digits in chunks ((\d{3})(\d{3})(\d{4})) and formats.

Here's the test:

test_data = {
  "+1 480-874-4666"                             => "480-874-4666",
  "404-581-4000"                                => "404-581-4000",
  "(805) 682-4726"                              => "805-682-4726",
  "978-851-7321, Ext 2606"                      => "978-851-7321",
  "413- 658-1100"                               => "413-658-1100",
  "(513) 287-7000,Toll Free (800) 733-2077"     => "513-287-7000",
  "1 (813) 274-8130"                            => "813-274-8130",
  "212-363-3200,Media Relations: 212-668-2251." => "212-363-3200",
  "323/221-2164"                                => "323-221-2164",
  ""                                            => nil,
  "foobar"                                      => nil,
  "1234567"                                     => nil,
}

test_data.each do |input, expected_output|
  extracted = extract_phone_number(input)
  print "FAIL (expected #{expected_output}): " unless extracted == expected_output
  puts extracted
end
like image 171
Ryan McGeary Avatar answered Sep 20 '22 13:09

Ryan McGeary