当前位置:  开发笔记 > 编程语言 > 正文

电话号码正则表达式不适用于swift

如何解决《电话号码正则表达式不适用于swift》经验,为你挑选了2个好方法。

我正在开发一个可以使用电话号码的应用程序,但我在使用正则表达式查找字符串中的电话号码时遇到了问题.

我的应用程序查看联系人的电话号码,该号码可以是:

(短划线" - ")
XXXX-XXXX
XXXXX-XXXXX
(YY)XXXX-XXXX
(YY)XXXXX-XXXX
+ ZZ(YY)XXXX-XXXX
+ ZZ(YY)XXXXX-XXXX


(不带破折号)
XXXXXXXX
XXXXXXXXX
(YY) XXXXXXXX
(YY)XXXXXXXXX
+ ZZ(YY)XXXXXXXX
+ ZZ(YY)XXXXXXXXX

基于上述所有可能性,我编写了以下代码:

    let range = telefone.rangeOfString("[0-9]{4,5}-[0-9]{4}", options:.RegularExpressionSearch)
    let range2 = telefone.rangeOfString("[0-9]{9}$", options:.RegularExpressionSearch)

    var found: String?

    if range != nil{
        found = telefone.substringWithRange(range!)
    }else if range2 != nil{
        found = telefone.substringWithRange(range2!)
    }

    print(found)

range是找到phoneNumbers的正则表达式(带短划线" - ")
range2是为电话号码提供资金的正则表达式(不带破折号)

使用此代码,我只获得电话号码,没有国家代码或区号.

问题是,当我使用
+ ZZ(YY)XXXXX-XXXX 等电话号码测试时,此代码在找到的变量上返回nil

有人可以帮我找到另一种写正则表达式的方法,只获取包含所有联系电话号码的字符串的"X"值吗?

更新:

我注意到上面的代码,变量范围,返回null

    var telefone = "+42 43 23123-2221"

    let range = telefone.rangeOfString("\\d{4,5}\\-?\\d{4}", options:.RegularExpressionSearch)

    print("range \(range)")   //here returns nil

操场



1> Amen JLILI..:

这可能不是最令人愉悦的正则表达式,但我相信它能完成这项工作.

(\+\d{2}\s*(\(\d{2}\))|(\(\d{2}\)))?\s*\d{4,5}\-?\d{4}

在此输入图像描述

演示:https://regex101.com/r/gM4uT3/3

编辑:

如果我理解正确的话,这只会匹配一个捕获组内没有任何区号等(仅限X)的电话号码.

(?:\+\d{2}\s*(?:\(\d{2}\))|(?:\(\d{2}\)))?\s*(\d{4,5}\-?\d{4})

在此输入图像描述

演示:https://regex101.com/r/gM4uT3/6



2> Dean Taylor..:

如果括号是选项,即."+42(43)23123-2221"或"+42 43 23123-2221":

(?:(\+\d\d\s+)?((?:\(\d\d\)|\d\d)\s+)?)(\d{4,5}\-?\d{4})

正则表达式可视化

或者,如果需要括号:

(?:(\+\d\d\s+)?(\(\d\d\)\s+)?)(\d{4,5}\-?\d{4})

正则表达式可视化

您可能希望根据间距要求\s+仅使用一个空间进行更换 depending on your spacing requirements.

Also note you might want to add anchors ^在开头和$结尾添加锚点以确保字符串完全匹配.

https://regex101.com/r/bR5uM3/2

http://swiftstub.com/44951173

var telefone = "+42 43 23123-2221"

// Use this regular expression to require "+42 (43) 23123-2221" brackets
//let phoneNumberRegEx = "(?:(\\+\\d\\d\\s+)?(\\(\\d\\d\\)\\s+)?)(\\d{4,5}\\-?\\d{4})";


// Use this regular expression to make them optional "+42 43 23123-2221" brackets
let phoneNumberRegEx = "(?:(\\+\\d\\d\\s+)?((?:\\(\\d\\d\\)|\\d\\d)\\s+)?)(\\d{4,5}\\-?\\d{4})";

let range = telefone.rangeOfString(phoneNumberRegEx, 
    options:.RegularExpressionSearch)

print("range \(range)")

var found = telefone.substringWithRange(range!)

print(found)



let regex = try! NSRegularExpression(pattern: phoneNumberRegEx, options: [])

let telephoneRange = NSMakeRange(0, telefone.characters.count)
let result = regex.firstMatchInString(telefone, options: NSMatchingOptions(rawValue: 0), range: telephoneRange)
let r1 = result!.rangeAtIndex(1)
let r2 = result!.rangeAtIndex(2)
let r3 = result!.rangeAtIndex(3)
if (r1.length > 0) {
    let phoneCountry = (telefone as NSString).substringWithRange(r1)
    print("country: \(phoneCountry)")
}
if (r2.length > 0) {
    let phoneArea = (telefone as NSString).substringWithRange(r2)
    print("area: \(phoneArea)")
}
if (r3.length > 0) {
    let phone = (telefone as NSString).substringWithRange(r3)
    print("phone: \(phone)")
}

这将返回以下结果:

range Optional(Range(0..<17))
+42 43 23123-2221
country: +42
area: 43
phone: 23123-2221

推荐阅读
N个小灰流_701
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有