码迷,mamicode.com
首页 > 编程语言 > 详细

swift的正则表达式(NSRegularExpression)

时间:2015-07-04 23:33:39      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:ios   正则表达式   swift   

swift的正则表达式(NSRegularExpression)

by 伍雪颖

import UIKit

class ViewController: UIViewController {

   
override func viewDidLoad() {
       
super.viewDidLoad()
       
let mailPattern =
       
"^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
       
let matcher = RegexHelper(mailPattern)
       
let maybeMailAddress = "lesvio@qq.com"
       
if matcher.match(maybeMailAddress) {
           
println("valid email")
        }
    }
   
   
struct RegexHelper {
       
let regex: NSRegularExpression?
       
init(_ pattern: String) {
           
var error: NSError?
           
regex = NSRegularExpression(
                pattern: pattern,
                options: .CaseInsensitive,
                error: &error)
        }
       
       
func match(input: String) -> Bool {
           
if let matches = regex?.matchesInString(input,
                options:
nil,
                range:
NSMakeRange(0, count(input))) {
               
return matches.count > 0
            }
else {
               
return false
            }
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

swift的正则表达式(NSRegularExpression)

标签:ios   正则表达式   swift   

原文地址:http://blog.csdn.net/rainlesvio/article/details/46759069

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!