Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string contains at least a upperCase letter, a digit, or a special character in Swift?

Tags:

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.

like image 325
Anish Parajuli 웃 Avatar asked Apr 09 '15 10:04

Anish Parajuli 웃


People also ask

How do you check if a string is uppercase?

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!

How do you check if there is a capital letter in a string Java?

To check whether a character is in Uppercase or not in Java, use the Character. isUpperCase() method.


1 Answers

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 
like image 55
Joshua Arvin Lat Avatar answered Sep 28 '22 03:09

Joshua Arvin Lat