Swift's regular expression (nsregularexpression) by Wusheying
ImportUIKit
classViewcontroller:Uiviewcontroller {
OverridefuncViewdidload () {
Super.Viewdidload()
LetMailpattern =
^ ([a-z0-9_\\.-]+) @ ([\\da-z\\.-]+) \ \. ([a-z\\.] {2,6}) $"
LetMatcher =Regexhelper(Mailpattern)
Letmaybemailaddress ="[email protected]"
ifMatcher.Match(maybemailaddress) {
println("valid email")
}
}
structRegexhelper {
LetRegex:nsregularexpression?
Init(_Pattern:String) {
varError:Nserror?
Regex =nsregularexpression(
Pattern:pattern,
Options:. Caseinsensitive,
Error: &error)
}
funcmatch (input:String),Bool{
if Letmatches =Regex?.matchesinstring(Input,
Options:Nil,
Range:Nsmakerange(0,Count(input))) {
returnmatches.Count>0
} Else {
returnfalse
}
}
}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Swift's regular expression (nsregularexpression)