您的位置:首页 > 移动开发 > Swift

SWIFT中正则表达式验证邮箱

2015-07-11 11:29 381 查看
在playground内写入以下代码,正则关键字跟其它语言的没什么区别

class Regex {
let internalExpression:NSRegularExpression
let pattern:String

init(pattern:String) {
self.pattern = pattern
var error:NSError?
self.internalExpression = NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive, error: &error)!
}

func match(input:String) -> Bool {
let matches = self.internalExpression.matchesInString(input, options: nil, range: NSMakeRange(0, count(input)))
return matches.count > 0
}
}

var email_regex = case Email = "^([a-zA-Z0-9]+([._\\-])*[a-zA-Z0-9]*)+@([a-zA-Z0-9])+(.([a-zA-Z])+)+$"

var regex = Regex(pattern:email_regex)

regex.match("service@t.com")  //RETURN true
regex.match("ken.ngai@tao.com.cn") //RETURN true
regex.match("buddy_wei@frend.org") //RETURN true


CaseInsensitive:大小写不敏感
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: