structRegexHelper{ letregex:NSRegularExpression? init(_pattern:String){ varerror:NSError? regex=NSRegularExpression(pattern:pattern,options:.CaseInsensitive,error:&error) } funcmatch(input:String)->Bool{ ifletmatches=regex?.matchesInString(input,options:nil,range:NSMakeRange(0,count(input))){ returnmatches.count>0 }else{ returnfalse } } } //验证方式 letmailPattern="^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$" letmatcher=RegexHelper(mailPattern) letmaybeMailAddress="123446@qq.com" ifmatcher.match(maybeMailAddress) { println("有效的邮箱地址") } else { println("无效的邮箱地址") }