3

My words (phone numbers) may have in the beginning "00" or "+".

Example number phone:

00xxx xx xxxxxxx
+xxx xx xxxxxxx
(+xxx) xx xxxxxxx
(00xxx) xx xxxxxxx

I have:

Regex regexObj = new Regex(@"^\(?[+( ]?([0-9]{3})\)?[) ]?([0-9]{2})[- ]?([0-9]{7})$");

if (regexObj.IsMatch(TextBox1.Text))
{
    // IF OK
    string formattedPhoneNumber = regexObj.Replace(TextBox1.Text, "(+$1) $2 $3");
}

How to put it in the regular expression? For now I can only put the "+", and "(" , ")"

thanks

VMAtm
  • 27,943
  • 17
  • 79
  • 125
YProgrammer
  • 855
  • 3
  • 8
  • 14

5 Answers5

2

I'd recommend going with libphonenumber from Google: http://blog.appharbor.com/2012/02/03/net-phone-number-validation-with-google-libphonenumber

friism
  • 19,068
  • 5
  • 80
  • 116
1

Break the job onto several steps.

The first step has to be to strip off the +353, 00 353, 011 353, +353 (0), 00 353 (0), 011 353 (0) or 0 prefix before investigating the rest of the number.

  Pattern: ^(\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)(353)\)?[\s-]?)?\(?0?(?:\)[\s-]?)?([1-9]\d{1,4}\)?[\d\s-]+)((?:x|ext\.?|\#)\d{3,4})?$

Use the above pattern to extract the '353' from $2 to know that international format was used, otherwise assume national format if $2 is null.

Extract the optional extension number details from $4 and store them for later use.

Extract the NSN (including spaces, hyphens and parentheses) from $3.

Remove those spaces, hyphens and parentheses and use another more detailed RegEx pattern to validate the NSN part of the number by length and initial digits.

Don't worry about users entering mismatched brackets or random punctuation. The goal is to ensure the user entered the right number of digits to make a valid phone number. Extract and validate that number, then clean it up for display using the correct formatting rules for each number range.

g1smd
  • 61
  • 1
  • 2
1

This optionally matches your 4 different phone prefix, followed by 2 digits, followed by 7 digits.

(?:(00\d{3})|(\+\d{3})|(\(\+\d{3}\))|(\(00\d{3}\)))\s(\d{2})\s(\d{7})

Blazes
  • 4,721
  • 2
  • 22
  • 29
1

how about this:

Regex regexObj = new Regex(@"^(?:\(?)(?:\+|0{2})([0-9]{3})\)? ([0-9]{2}) ([0-9]{7})$");

EDIT:

^(?:\((?:\+|00)([0-9]{3})\)|(?:\+|00)([0-9]{3}))? ([0-9]{2})[- ]?([0-9]{7})$
Beno
  • 4,633
  • 1
  • 27
  • 26
  • This answer incorrectly matches: "00353) 86 5551212", "(00353 86 5551212", "+00353 86 5551212" – Blazes May 30 '11 at 13:11
  • i found that it matched "00353) 86 5551212", "(00353 86 5551212" just fine. I admit that "+00xxx xx xxxxxxx" was a problem and have updated it accordingly – Beno May 30 '11 at 19:00
  • But @Beno - that's not a valid phone number... – Blazes May 31 '11 at 09:17
  • finally get what you are saying - your first comment confused me since "+00353 86 5551212" doesn't match that first regex. I have updated the answer – Beno May 31 '11 at 13:08
1

Tested with your examples:

Regex regexObj = new Regex(@"^(?:(?:\+|00)([0-9]{3})|\((?:\+|00)([0-9]{3})\))[- ]?([0-9]{2})[- ]?([0-9]{7})$");

if (regexObj.IsMatch(TextBox1.Text))
{
    //IF OK
    string formattedPhoneNumber = regexObj.Replace(TextBox1.Text, "(+$1$2) $3 $4");
}

Explanation:

(?:\+|00): + or 00 - no capture

(?:(?:\+|00)([0-9]{3})|\((?:\+|00)([0-9]{3})\)): +xxx or 00xxx or (+xxx) or (00xxx), capture only the numerals. It's important to separate out the ( ) matches so that if parenthesis are included, both must be present for a match (ie (+999 99 9999999 would be invalid.

"(+$1$2) $3 $4": The match will either capture the first or second ([0-9]{3}) group but not both - have to combine them here.

ic3b3rg
  • 14,629
  • 4
  • 30
  • 53