如何匹配 Swift 中两个相似的 phone 号码,但其中一个包含国家代码,另一个包含或不包含国家代码

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

我正在尝试匹配 swift 中两个相似的 phone 号码,但其中一个号码前面有国家/地区代码,而另一个可能有所不同。 例如,我希望这 2 个 phone 数字匹配:

0499999999

+32499999999

我希望这对全世界的任何 phone 号码都有效。是否有用于此的正则表达式或者我可以为此下载的 pod?

谢谢

您可以查找 table 国家和国家代码并从 phone 中检查他们的语言环境是什么

let countrycode = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as? String

let countryPrefixes = ["IE": "+353", 
                       "US": "+1",
                        etc...

let prefix = countryPrefixes[countrycode]

获得前缀后,您应该可以只查看剩余的数字。

或者您可以使用这个 github 库 https://github.com/rmaddy/RMPhoneFormat,它似乎已经为您完成了这项工作。

当然,这是假设您希望在您的设备中输入一个 phone 号码,该号码在 phone 运行的区域设置中有效。

使用 PhoneNumberKit 解析电话号码。

https://github.com/marmelroy/PhoneNumberKit

赞:

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)