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 checkTextSufficientComplexity(var text : String) -> Bool{ let capitalLetterRegEx = "[A-Z]+" var texttest = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx) var capitalresult = texttest.evaluateWithObject("AniP") 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 %@", numberRegEx) var specialresult = texttest2.evaluateWithObject(text) println("\(specialresult)") return capitalresult && numberresult && specialresult }
The problem is the below regular expression [A-Z]+ returns true for only e.g AVATAR and returns false for Avatar. I want my regular expression return true if it contains at least one UpperCase in String.
To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase. Copied!
To check whether a character is in Uppercase or not in Java, use the Character. isUpperCase() method.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With