Search code examples
iosregexswiftcontacts

How would you match two similar phone numbers in Swift, but one contains the country code and the other one does or does not


I am trying to match two similar phone numbers in swift, but one of them is preceded by a country code and the other one can vary. For instance, I want these 2 phone numbers to match:

0499999999

+32499999999

I want this to be valid for any phone number all over the world. Is there a regex for this or maybe a pod I could download for doing this?

Thanks


Solution

  • Use PhoneNumberKit To Parse PhoneNumber.

    https://github.com/marmelroy/PhoneNumberKit

    Like :

    let rawNumberArray = ["0291 12345678", "+49 291 12345678", "04134 1234", "09123 12345"]
    let phoneNumbers = phoneNumberKit.parse(rawNumberArray)
    let phoneNumbersCustomDefaultRegion = phoneNumberKit.parse(rawNumberArray, withRegion: "DE",  ignoreType: true)