在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:大小写不敏感