Alex Rivera | Logout

Regular expression for Iranian mobile phone numbers?

Asked 2011-07-20T12:13:37.397
32

How can I validate mobile numbers with a regular expression? Iran Mobile phones have numeral system like this:

091- --- ----
093[1-9] --- ----

Some examples for prefixes:

0913894----
0937405----
0935673---- 
0912112----

Source: http://en.wikipedia.org/wiki/Telephone_numbers_in_Iran

Edit
Report

1 Answer

0

^09(1\d|3[1-9])-\d{3}-\d{4}$

is the regular expression that you need:

Starting with the beginning of the string (so no other digits sneak in)

09 is constant

1 can be combined with any subsequent digit, 3 cannot be combined with 0

-, and 3 digits

-, and 4 digits

End of string (again, so no other digits sneak in)

... but don't take my word for it, run it (I would recommend adding more test numbers):

        List<string> possible = new List<string> { "091-555-8888", 
"0915-888-4444", 
"0930-885-8844", 
"0955-888-8842" };

        string regex = @"09(1\d|3[1-9])-\d{3}-\d{4}$";

        foreach (string number in possible)
        {
            Console.WriteLine("{0} is{1} an Iranian number", number, Regex.IsMatch(number, regex) ? "" : " not");
        }
answered 2011-10-27T08:46:57.863

Your Answer