jeffward01 / phone-number-parser

A .NET library for parsing phone numbers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Phone Number Parser

A library for parsing phone numbers with nullable reference type annotations, built for:

  • .NET 7.0
  • .NET Standard 2.1
  • .NET Standard 2.0

License: MIT GitHub last commit Build Status NuGet GitHub Release Date NuGet

This library provides a number of benefits over a regular expression, for example greater validity of phone numbers including national destination codes (area codes) and subscriber numbers based upon published numbering plans for each country. Additional attributes such as the kind of phone number (Mobile, Geographic or Non-Geographic) are also included, and all parsing is performed locally within the library using embedded in-memory data files.

The library does not:

  • Provide certainty that a phone number is assigned and in use
  • Include the original carrier for mobile phone numbers due to number portability in most countries
  • Support extension numbers

Install

Install via nuget:

dotnet add package PhoneNumberParser

Add the namespace:

using PhoneNumbers;

Parsing

Parsing a phone number is achieved via the PhoneNumber.Parse method (or alternatively via PhoneNumber.TryParse). Any spaces, hyphens or other formatting in the input string is ignored.

There are 2 overloads for Parse:

// If the phone number string is in international format (e.g. +XX):
var phoneNumber = PhoneNumber.Parse("+441142726444");

// If the phone number string is not in international format:
// Specify the ISO 3166 Alpha-2 code for the country as the second parameter.
var phoneNumber = PhoneNumber.Parse("01142726444", "GB");

There are 3 overloads for TryParse:

// If the phone number string is in international format (e.g. +XX):
PhoneNumber.TryParse("+442079813000", out PhoneNumber phoneNumber);

// If the phone number string is not in international format:
// Specify the ISO 3166 Alpha-2 code for the country as the second parameter.
PhoneNumber.TryParse("01142726444", "GB", out PhoneNumber phoneNumber);

// The phone number string is not in international format but the country code is not known:
PhoneNumber.TryParse("02079813000", out IEnumerable<PhoneNumber> phoneNumbers);

The resulting PhoneNumber has the following properties:

// PhoneNumber properties:
phoneNumber.Country.CallingCode;                // +44
phoneNumber.Country.Continent;                  // Europe
phoneNumber.Country.HasNationalDestinationCodes // true
phoneNumber.Country.InternationalCallPrefix;    // 00 (obsolete)
phoneNumber.Country.Iso3166Code;                // GB
phoneNumber.Country.Name;                       // United Kingdom
phoneNumber.Country.SharesCallingCode           // true
phoneNumber.Country.TrunkPrefix;                // 0
phoneNumber.NationalDestinationCode;            // 114
phoneNumber.NationalSignificantNumber           // 1142726444
phoneNumber.PhoneNumberKind;                    // PhoneNumberKind.GeographicPhoneNumber
phoneNumber.SubscriberNumber                    // 2726444

// There are 3 subclasses of PhoneNumber, the correct type to cast to
// can be determined by inspecting the PhoneNumberKind property.

// If PhoneNumberKind.GeographicPhoneNumber
var geographicPhoneNumber = (GeographicPhoneNumber)phoneNumber;
geographicPhoneNumber.GeographicArea;           // Sheffield

// If PhoneNumberKind.MobilePhoneNumber
var mobilePhoneNumber = (MobilePhoneNumber)phoneNumber;
mobilePhoneNumber.IsDataOnly;                   // true/false (obsolete)
mobilePhoneNumber.IsPager;                      // true/false
mobilePhoneNumber.IsVirtual;                    // true/false

// If PhoneNumberKind.NonGeographicPhoneNumber
var nonGeographicPhoneNumber = (NonGeographicPhoneNumber)phoneNumber;
nonGeographicPhoneNumber.IsFreephone;           // true/false
nonGeographicPhoneNumber.IsMachineToMachine;    // true/false
nonGeographicPhoneNumber.IsPremiumRate;         // true/false
nonGeographicPhoneNumber.IsSharedCost;          // true/false

The phone number can be formatted in the following ways, the default format output can be round tripped via PhoneNumber.Parse() to make serialization or database persistence straightforward.

phoneNumber.ToString();                         // +441142726444   (defaults to E.164 format)
phoneNumber.ToString("E.164");                  // +441142726444   (E.164 format)
phoneNumber.ToString("E.123");                  // +44 114 2726444 (E.123 international format)
phoneNumber.ToString("N");                      // (0114) 2726444  (E.123 national notation format)

ParseOptions

The ParseOptions class can be used to control parsing, the defaults can be configured via:

ParseOptions.Default

At present, the only options available are which countries are parsed.

By default all countries supported by the library can be parsed and any future supported ones will be automatically included.

Opt-in

To support parsing specific countries only, and ignore by default any new ones added in future versions of the library:

ParseOptions.Default.Countries.Clear():
ParseOptions.Default.Countries.Add(CountryInfo.X);

To opt in to all countries supported by the library within a continent:

ParseOptions.Default.Countries.Clear():

// One or more continent can be added.
ParseOptions.Default.AllowAfricanCountries();
ParseOptions.Default.AllowAsianCountries();
ParseOptions.Default.AllowEuropeanCountries();

Opt-out

To out out of specific countries but still use any new ones added in future versions of the library:

ParseOptions.Default.Countries.Remove(CountryInfo.X);

Country support

The library currently supports parsing phone numbers for the following countries and although best endeavours are made to adhere to published telephone numbering plans, depending on the accessibility of data there may be discrepancies. If you happen to find any, please raise an issue.

Africa

Country ISO 3166 Code Calling Code Trunk Prefix Geographic Mobile Mobile
(Data Only)
Mobile
(Pager)
Mobile
(Virtual)
Non-Geographic Non-Geographic
(Freephone)
Non-Geographic
(Premium Rate)
Non-Geographic
(Shared Cost)
Non-Geographic (M2M)
Egypt EG +20 0 Yes Yes Yes Yes
Nigeria NG +234 0 Yes Yes
South Africa Za +27 0 Yes Yes Yes Yes Yes Yes Yes

Asia

Country ISO 3166 Code Calling Code Trunk Prefix Geographic Mobile Mobile
(Data Only)
Mobile
(Pager)
Mobile
(Virtual)
Non-Geographic Non-Geographic
(Freephone)
Non-Geographic
(Premium Rate)
Non-Geographic
(Shared Cost)
Non-Geographic (M2M)
Hong Kong HK +852 Yes Yes Yes Yes Yes
Macau MO +853 Yes Yes
Singapore SG +65 Yes Yes Yes Yes

Europe

Country ISO 3166 Code Calling Code Trunk Prefix Geographic Mobile Mobile
(Data Only)
Mobile
(Pager)
Mobile
(Virtual)
Non-Geographic Non-Geographic
(Freephone)
Non-Geographic
(Premium Rate)
Non-Geographic
(Shared Cost)
Non-Geographic (M2M)
Austria AT +43 0 Yes Yes Yes Yes Yes
Belarus BY +375 8 Yes Yes Yes Yes Yes
Belgium BE +32 0 Yes Yes Yes Yes Yes Yes
Bulgaria BG +359 0 Yes Yes Yes Yes Yes Yes Yes
Croatia HR +385 0 Yes Yes Yes Yes Yes Yes Yes
Czech Republic CZ +420 Yes Yes Yes Yes Yes Yes Yes
Denmark DK +45 Yes Yes Yes Yes Yes
Estonia EE +372 Yes Yes Yes Yes Yes Yes
France FR +33 0 Yes Yes Yes Yes Yes Yes
Finland FI +358 0 Yes Yes Yes Yes Yes
Germany DE +49 0 Yes Yes Yes Yes Yes Yes Yes Yes
Gibraltar GI +350 Yes Yes Yes Yes
Greece GR +30 Yes Yes Yes Yes Yes Yes Yes Yes
Guernsey GG +44 0 Yes Yes
Hungary HU HU 06 Yes Yes Yes Yes Yes Yes
Ireland IE +353 0 Yes Yes Yes Yes Yes Yes Yes Yes Yes
Isle of Man IM +44 0 Yes Yes
Italy IT +39 Yes Yes Yes Yes Yes Yes
Jersey JE +44 0 Yes Yes
Kosovo XK +383 0 Yes Yes Yes Yes Yes Yes
Moldova MD +373 0 Yes Yes Yes Yes Yes Yes
Monaco MC +377 Yes Yes
Netherlands NL +31 0 Yes Yes Yes Yes Yes Yes Yes
Norway NO +47 Yes Yes Yes
Poland PL +48 Yes Yes Yes Yes
Portugal PT +351 Yes Yes Yes Yes Yes Yes Yes
Romania RO +40 0 Yes Yes Yes Yes Yes Yes Yes
San Marino SM +378 Yes Yes Yes
Serbia RS +381 0 Yes Yes Yes Yes Yes Yes
Spain ES +34 Yes Yes Yes Yes Yes Yes
Slovakia SK +421 0 Yes Yes Yes Yes Yes Yes
Sweden SE +46 0 Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes
Switzerland CH +41 0 Yes Yes Yes Yes
Ukraine UA +380 0 Yes Yes Yes Yes Yes
United Kingdom GB +44 0 Yes Yes Yes Yes Yes Yes Yes Yes

Notes

  • The ISO code for the United Kingdom is 'GB' rather than 'UK'.
  • Geographically assigned numbers in France are currently only within the top level geographic zones (01 Île-de-France, 02 Nord-Ouest, 03 Nord-Est, 04 Sud-Est and 05 Sud-Ouest).
  • Where possible, the geographic area name is in the language/locality of the country for the phone number (e.g. for an Italian phone number assigned to Florence, the geographic area will be set to Firenze.

References for number data

These were used as the references for the phone number data for each country.

Austria

Belarus

Belgium

Bulgaria

Croatia

Czech Republic

Denmark

Egypt

Estonia

Finland

France

Germany

Gibraltar

Greece

Hong Kong

Hungary

Ireland

Italy

Kosovo

Macau

Moldova

Monaco

Netherlands

Nigeria

Norway

Poland

Portugal

Romania

San Marino

Serbia

Singapore

Slovakia

South Africa

Spain

Sweden

Switzerland

Ukraine

United Kingdom, Guernsey, Jersey, Isle of Man

About

A .NET library for parsing phone numbers.

License:MIT License


Languages

Language:C# 99.9%Language:PowerShell 0.0%Language:Shell 0.0%