Phone Number Parsing
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Home Report An Issue Rollable ExpressionsRecursionPhone Number Parsing
You have been provided a large string of phone numbers. The problem is, each phone number does not follow a standard format.
You have been asked to convert them from the non standardized format into a standard format.
Phone numbers will be given in the following formats:
-
1231231234
-
123 123 1234
-
(123)-123-1234
-
(123) 123 1234
-
(123)123-1234
You need to convert them into the format 123-123-1234
.
text = """
1231231234
123 123 1234
(123)-123-1234
(123) 123 1234
(123)123-1234
"""
PhoneNumber.parse(text)
"""
123-123-1234
123-123-1234
123-123-1234
123-123-1234
123-123-1234
"""
Hint
Consider using regular expressions with digits and capture groups to replace phone number expressions in the string.
You can replace expressions one at a time, rather than all at once.
Example Solution
defmodule PhoneNumber
def parse(string) do
replacement = "\\1-\\2-\\3"
string
|> String.replace(~r/(\d{3})(\d{3})(\d{4})/, replacement)
|> String.replace(~r/(\d{3}) (\d{3}) (\d{4})/, replacement)
|> String.replace(~r/\((\d{3})\)-(\d{3})-(\d{4})/, replacement)
|> String.replace(~r/\((\d{3})\) (\d{3}) (\d{4})/, replacement)
|> String.replace(~r/\((\d{3})\)(\d{3})-(\d{4})/, replacement)
end
end
Implement the PhoneNumber
module as documented below.
defmodule PhoneNumber do
@moduledoc """
Documentation for `PhoneNumber`
"""
@doc """
Parse phone numbers and convert them to XXX-XXX-XXXX format.
## Examples
iex> PhoneNumber.parse("1231231234")
"123-123-1234"
iex> PhoneNumber.parse("123 123 1234")
"123-123-1234"
iex> PhoneNumber.parse("(123)-123-1234")
"123-123-1234"
iex> PhoneNumber.parse("(123) 123 1234")
"123-123-1234"
iex> PhoneNumber.parse("(123)123-1234")
"123-123-1234"
iex> PhoneNumber.parse("1231231234 4564564567")
"123-123-1234 456-456-4567"
"""
def parse(string) do
end
end
Commit Your Progress
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status
to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum
folder to commit your progress.
$ git add .
$ git commit -m "finish Phone Number Parsing exercise"
$ git push
We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.