user1320651
user1320651

Reputation: 836

regular expressions dial code patterns

I want to transform dialing numbers from a range of inputs that are varied

If I want to take numbers such as

 +65 12345678
 +44 12345678
 +852 12345678

then transform them to become for all +65 numbers

12345678

+44 numbers become

001 44 12345678

then +852 numbers become

*852 12345678

and at the same time removing spaces and other funny chars such as hyphens or periods ("-", ".") what is the best regex to do this or the best solution in c#?

Upvotes: 0

Views: 166

Answers (2)

user1320651
user1320651

Reputation: 836

Eventually I went for the ported version of google libphonenumber

bitbucket.org/pmezard/libphonenumber-csharp/wiki/Home

    private DialedNumber applyCallPattern(string noToDial)
    {

        noToDial = noToDial.Replace("callto:", ""); 
       //libphone removes text anyway so this line above is not needed


        PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance();
        string dc = "SG";
        if (AutoDialer.Properties.Settings.Default.BaseOffice == "Hong Kong")
        {
            dc = "HK";
        }
        PhoneNumber pn = phoneUtil.Parse(noToDial, dc);
        string rc = phoneUtil.GetRegionCodeForNumber(pn);
        string dialingNumber = null;



        if (rc == "SG")
        {
            dialingNumber = phoneUtil.Format(pn, PhoneNumberFormat.NATIONAL);
            if (AutoDialer.Properties.Settings.Default.BaseOffice == "Hong Kong")
            {
                dialingNumber = "*65" + dialingNumber;
            }
        }

        else if (rc == "HK")
        {
            dialingNumber = phoneUtil.Format(pn, PhoneNumberFormat.NATIONAL);
            if (AutoDialer.Properties.Settings.Default.BaseOffice == "Singapore")
            {
                dialingNumber = "*852" + dialingNumber;
            }
        }

        else
        {
            dialingNumber = phoneUtil.Format(pn, PhoneNumberFormat.E164);
            dialingNumber = dialingNumber.Replace("+", "001");
        }
        dialingNumber = dialingNumber.Replace(" ", "");
        DialPopup popup = new DialPopup();
        popup.label1.Text = "Calling: " + dialingNumber;
        popup.Show();

        DialedNumber dn = new DialedNumber(dialingNumber, phoneUtil.GetRegionCodeForNumber(pn), phoneUtil.GetNumberType(pn).ToString(), DateTime.Now, false);
        Program.lastNoDialed = dialingNumber;
        return dn;
    }

libphonenumber rocks and makes it SOOOOOOOOOOOOOOOOOOOOOOO much easier, so thanks for the suggestion Alden, really helped.

For anyone else doing this task, I havent found anything anywhere near as good as the libphonenumber ported version. It strips out any junk and totally nails everything I needed with ease. Also able to get line type (mobile, fixed, premium etc), region code for dialing plus more. Job done thanks Google.

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You can do this using delegate:

Regex reg = new Regex(@"\+(\d{2,3})[-. ](\d{8})");
string result = reg.Replace(input, delegate(Match m) {
    switch (m.Groups[1].Value) {
        case "65":  return m.Groups[2].Value;
                    break;
        case "44":  return "001 44 " + m.Groups[2].Value;
                    break;
        case "852": return "*852 " + m.Groups[2].Value;
                    break;
        default:    return m.Value;
                    break;
    }
});

Upvotes: 1

Related Questions