在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 = "^[a-zA-Z0-9]+([._\\-])*[a-zA-Z0-9]*@([a-zA-Z0-9])+(.([a-zA-Z])+)+$" var regex = Regex(pattern:email_regex) regex.match("[email protected]") //RETURN trueregex.match("[email protected]") //RETURN trueregex.match("[email protected]") //RETURN true
时间: 2024-09-30 15:44:53