I am trying to create a method that finds whether a string contains a number , Upper case letter and a special character using regular expression as below
func
Simply replace your RegEx rule [A-Z]+ with .*[A-Z]+.* (and other RegEx rules as well)
Rules
[A-Z]+ matches only strings with all characters capitalized
Examples: AVATAR, AVA, TAR, AAAAAA
Won't work: AVATAr
.* matches all strings (0+ characters)
Examples: 1, 2, AVATAR, AVA, TAR, a, b, c
.*[A-Z]+.* matches all strings with at least one capital letter
Examples: Avatar, avataR, aVatar
Explanation:
I. .* will try to match 0 or more of anything
II. [A-Z]+ will require at least one capital letter (because of the +)
III. .* will try to match 0 or more of anything
Avatar [empty | "A" | "vatar"]
aVatar ["a" | "V" | "atar"]
aVAtar ["a" | "VA" | "tar"]
Working Code
func checkTextSufficientComplexity(var text : String) -> Bool{
let capitalLetterRegEx = ".*[A-Z]+.*"
var texttest = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx)
var capitalresult = texttest!.evaluateWithObject(text)
println("\(capitalresult)")
let numberRegEx = ".*[0-9]+.*"
var texttest1 = NSPredicate(format:"SELF MATCHES %@", numberRegEx)
var numberresult = texttest1!.evaluateWithObject(text)
println("\(numberresult)")
let specialCharacterRegEx = ".*[!&^%$#@()/]+.*"
var texttest2 = NSPredicate(format:"SELF MATCHES %@", specialCharacterRegEx)
var specialresult = texttest2!.evaluateWithObject(text)
println("\(specialresult)")
return capitalresult || numberresult || specialresult
}
Examples:
checkTextSufficientComplexity("Avatar") // true || false || false
checkTextSufficientComplexity("avatar") // false || false || false
checkTextSufficientComplexity("avatar1") // false || true || false
checkTextSufficientComplexity("avatar!") // false || false || true