swift正则表达式的几种方式

来源:互联网 发布:网络语挖煤是什么意思 编辑:程序博客网 时间:2024/06/05 19:01

Swift虽然是一个新出的语言,但却不提供专门的处理正则的语法和类。所以我们只能使用古老的NSRegularExpression类进行正则匹配。 
即先接受一个正则表达式的字符串,由此生成NSRegularExpression对象,再用该对象来匹配输入字符串,返回匹配结果。

1,为便于使用,定义一个正则匹配工具类(内部封装使用NSRegularExpression) 
代码如下

import Foundationstruct MyRegex {    let regex: NSRegularExpression?    init(_ pattern: String) {        regex = try? NSRegularExpression(pattern: pattern, options: .CaseInsensitive)    }    func match(input: String) -> Bool {        if let matches = regex?.matchesInString(input,            options: [],            range: NSMakeRange(0, (input as NSString).length)) {                return matches.count > 0        }        else {            return false        }    }}import UIKitclass ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()        let mailPattern = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"        let matcher = MyRegex(mailPattern)        let maybeMailAddress = "admim"        if matcher.match(maybeMailAddress) {            print("邮箱地址格式正确")        }        else{            print("邮箱地址格式有误")        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

2,自定义 =~ 运算符,方便正则匹配 
像Ruby和Perl这样的语言,都有专门的=~运算符来进行正则匹配。而在swift中,我们可以通过操作符的重载来实现。

import Foundationstruct MyRegex {    let regex: NSRegularExpression?    init(_ pattern: String) {        regex = try? NSRegularExpression(pattern: pattern, options: .CaseInsensitive)    }    func match(input: String) -> Bool {        if let matches = regex?.matchesInString(input,            options: [],            range: NSMakeRange(0, (input as NSString).length)) {                return matches.count > 0        }        else {            return false        }    }}infix operator =~ {associativity noneprecedence 130}func =~ (lhs: String, rhs: String) -> Bool {    return MyRegex(rhs).match(lhs) //需要前面定义的MyRegex配合}import UIKitclass ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()        if "admin@hangge.com" =~ "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"{            print("邮箱地址格式正确")        }else{            print("邮箱地址格式有误")        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

3,其他常用格式验证的正则表达式 
举例使用如下

//用户名验证(允许使用小写字母、数字、下滑线、横杠,一共3~16个字符)^[a-z0-9_-]{3,16}$//Email验证^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$//手机号码验证^1[0-9]{10}$//URL验证^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$//IP地址验证^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$//html标签验证^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$
原创粉丝点击