UK phone number format string

I am looking for a procedure that will format a string of numbers as a UK phone number. The routine should take into account UK codes that require different formats (i.e. London compared to Edinburgh compared to Worcester), as well as mobile phone numbers.

My phone numbers are stored in the database as strings containing only numeric characters.

So far I have come up with this, but performance seems to be poor.

/// <summary> /// Formats a string as a UK phone number /// </summary> /// <remarks> /// 02012345678 becomes 020 1234 5678 /// 01311234567 becomes 0131 123 4567 /// 01905123456 becomes 01905 123456 /// 07816123456 becomes 07816 123456 /// </remarks> public static string FormatPhoneNumber(string phoneNumber) { string formattedPhoneNumber = null; if (!string.IsNullOrEmpty(phoneNumber)) { System.Text.RegularExpressions.Regex area1 = new System.Text.RegularExpressions.Regex(@"^0[1-9]0"); System.Text.RegularExpressions.Regex area2 = new System.Text.RegularExpressions.Regex(@"^01[1-9]1"); string formatString; if (area1.Match(phoneNumber).Success) { formatString = "0{0:00 0000 0000}"; } else if (area2.Match(phoneNumber).Success) { formatString = "0{0:000 000 0000}"; } else { formatString = "0{0:0000 000000}"; } formattedPhoneNumber = string.Format(formatString, Int64.Parse(phoneNumber)); } return formattedPhoneNumber; } 

Thoughts welcomed how to improve this ...

Edit

My initial thoughts are that I should store phone numbers as number fields in the database, then I can do without Int64.Parse and know that they are really numeric.

Edit 2

All phone numbers will be displayed in the UK or the UK, so special cases such as 0800 should not be considered.

+4
source share
4 answers

Phone numbers in the UK range from 7 digits to 10 digits, not counting the initial zero. "area" codes can vary from 2 to 4 (but sometimes 5) digits.

All tables showing the area code and the total length for each number prefix can be found on the OFCOM website. NB: these tables are very long.

In addition, there is no standard in which places are precisely indicated. Some people may put them in different places depending on how he “reads” the text he receives.

+10
source

** I'm looking for a procedure that will format a string of numbers as a UK phone number. **

You can download the Ofcom database, which lists the formats for each range of numbers, including national number numbers, and searches for each number that needs to be formatted. The database lists SABCDE digits and format: 0 + 10, 2 + 8, 3 + 7, 4 + 6, 4 + 5, 5 + 5 or 5 + 4 for each range.

There are a small number of errors in the database (especially for 01697 and 0169 77 codes), but they contain less than ten errors in more than a quarter of a million entries.

There are four files covering numbers 01 and 02, and separate files for different ranges of non-geographical numbers.

0 + 10 numbers are "National Dialing Only" and are written without parentheses around part of the area code. The area code will be 02x for all numbers 02, 01xx for all numbers 011x and 01x1, and 01xxx for most other numbers 01 (a very small number - about ten - will be 01xx xx).

Parentheses surround the area code for all other numbers 01 and 02 (that is, use parentheses to numbers 01 and 02, where part of the local number does not start with 0 or 1). The brackets show that local dialing is possible within the same area, omitting the numbers enclosed in parentheses.

The nomenclature 2 + 8 shows the area code and the length of the local number, and the entry 2075: 2 + 8 means that the number is formatted as (020) 75xx xxxx. Remember that the leading zero is not “counted” in the definition of 2 + 8.

** UK phone numbers vary in length from 8 digits to 12 digits **

No. Since 2000, most of them have 10 digits after the code "0". Some of them still have 9 digits after the trunk code "0".

There are also several special rooms, such as 0800 1111 and 0845 4647.

Codes

** "area" can vary from 2 to 4 digits. **

Area codes can vary from 2 to 5 digits (leading zero is not counted). To be clear, “020” is classified as a 2-digit area code, because the leading 0 is actually a trunk code. There are also area codes 011x and 01x1, and most other numbers have area codes 01xxx. The latter can have local numbers, the length of which is only 5 digits instead of the more widely used 6-digit local numbers. A very small number has an area code of 01xx xx and they have 5 or 4 digit local numbers.

** Also, there is no standard for where spaces will be placed. **

Between the area code part and the local number part there is always a space for all numbers 01 and 02.

Traditionally, for area codes (01xxxx), there is a space inside the area code, as shown in the figure. This represents the old local exchange groups in which this system is still used. Other (shorter) area codes are not separated.

Local numbers with 7 or 8 digits are divided up to the fourth digit from the end. Local numbers with 4, 5 or 6 digits are not divided. This also applies to geographic and non-geographic numbers.

For most numbers 03, 08 and 09, the number is written as 0xxx xxx xxxx.

Some 0800 and all 0500 numbers are written 0xxx xxxxxx.

For numbers 055, 056 and 070, the number is recorded 0xx xxxx xxxx.

For mobile phones and pager numbers, use 07xxx xxxxxx.

** except that some use '08000 abc def' instead of '0800 0abc def' **

This is a misuse. Keep in mind that some 0800 numbers have 9 digits after the trunk code 0, while others have 10 digits after the trunk code.

Thus, both 0800 xxxxxx and 0800 xxx xxxx are correct.

0500 numbers use only 0500 xxxxxx.

Most of the numbers 03, 08 and 09 are written as 0xxx xxx xxxx.

See also: http://en.wikipedia.org/wiki/Local_conventions_for_writing_telephone_numbers#United_Kingdom

+2
source

I spent some time through the OFCOM sheets and came up with the following.

 public static class TelephoneHelper { #region Regex Patterns private static readonly Regex[] patterns = { new Regex(@"(?<first>013873)(?<second>\d{5})"), new Regex(@"(?<first>015242)(?<second>\d{5})"), new Regex(@"(?<first>015394)(?<second>\d{5})"), new Regex(@"(?<first>015395)(?<second>\d{5})"), new Regex(@"(?<first>015396)(?<second>\d{5})"), new Regex(@"(?<first>016973)(?<second>\d{5})"), new Regex(@"(?<first>016974)(?<second>\d{5})"), new Regex(@"(?<first>016977)(?<second>\d{4}\d?)"), new Regex(@"(?<first>017683)(?<second>\d{5})"), new Regex(@"(?<first>017684)(?<second>\d{5})"), new Regex(@"(?<first>017687)(?<second>\d{5})"), new Regex(@"(?<first>019467)(?<second>\d{5})"), new Regex(@"(?<first>02\d)(?<second>\d{4})(?<third>\d{4})"), new Regex(@"(?<first>03\d{2})(?<second>\d{3})(?<third>\d{4})"), new Regex(@"(?<first>0500\d{6})"), new Regex(@"(?<first>05\d{3})(?<second>\d{6})"), new Regex(@"(?<first>07\d{3})(?<second>\d{6})"), new Regex(@"(?<first>08\d{2})(?<second>\d{3})(?<third>\d{3}\d?)"), new Regex(@"(?<first>09\d{2})(?<second>\d{3})(?<third>\d{4})"), new Regex(@"(?<first>01\d1)(?<second>\d{3})(?<third>\d{4})"), new Regex(@"(?<first>011\d)(?<second>\d{3})(?<third>\d{4})"), new Regex(@"(?<first>01\d{3})(?<second>\d{5}\d?)") }; #endregion public static string FormatAsUkTelephone(this string number) { Regex matchedPattern = null; foreach (Regex pattern in patterns) { if (pattern.IsMatch(number)) { matchedPattern = pattern; break; } } if (matchedPattern != null) { var mc = matchedPattern.Matches(number); if (mc[0].Groups.Count == 3) { return String.Format("{0} {1}", mc[0].Groups["first"], mc[0].Groups["second"]); } else if (mc[0].Groups.Count == 4) { return String.Format("{0} {1} {2}", mc[0].Groups["first"], mc[0].Groups["second"], mc[0].Groups["third"]); } } return number; } 
+2
source

I will be tempted to use a more stringent set of rules that test the bear’s minimum; Thus, assuming the leading zero is in the database, the pseudo-code will be:

 if( phoneNumber.substring(1,1) == "2" ) { // 000 0000 0000 } else if( phoneNumber.substring(1,1) == "1" && (phoneNumber.substring(1,1) == "2" || phoneNumber.substring(3,1) = "1") ) { // 0000 000 0000 } else { // 00000 000000 } 

NB. your templates are a little wrong 023 is a three-digit code, and 0800 is not

+1
source

All Articles