检查一个字符串是否至少包含一个在Swift中的upperCase字母,数字或特殊字符?

时间:2015-04-09 10:19:06

标签: ios regex swift

我正在尝试创建一个方法,用于查找字符串是否包含数字,大写字母和使用正则表达式的特殊字符,如下所示

 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

    }

问题是下面的正则表达式[A-Z] +仅为例如AVATAR返回true,并为Avatar返回false。我希望我的正则表达式返回true,如果它在String中包含至少一个UpperCase。

4 个答案:

答案 0 :(得分:32)

只需将您的RegEx规则[A-Z] +替换为。* [A-Z] +。*(以及其他RegEx规则)

<强>规则

  

[A-Z] +仅匹配所有字符大写的字符串

示例:AVATAR,AVA,TAR,AAAAAA
无法工作:AVATAr

  

。*匹配所有字符串(0+个字符)

示例:1,2,AVATAR,AVA,TAR,a,b,c

  

。* [A-Z] +。*匹配所有至少有一个大写字母的字符串

示例:Avatar,avataR,aVatar

说明:

予。 。*将尝试匹配0或更多的任何内容 II。 [A-Z] +将需要至少一个大写字母(因为+)
III。 。*将尝试匹配0或更多的任何内容

  

头像[空| &#34; A&#34; | &#34; vatar&#34;]
  aVatar [&#34; a&#34; | &#34; V&#34; | &#34; ATAR&#34;]
  aVAtar [&#34; a&#34; | &#34; VA&#34; | &#34; tar&#34;]

工作代码

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

}

<强>示例:

checkTextSufficientComplexity("Avatar") // true || false || false
checkTextSufficientComplexity("avatar") // false || false || false
checkTextSufficientComplexity("avatar1") // false || true || false
checkTextSufficientComplexity("avatar!") // false || false || true

答案 1 :(得分:10)

以下是Swift 3中Joshuas答案的简明版本,假设必须完成所有验证。

func validate(password: String) -> Bool {
    let capitalLetterRegEx  = ".*[A-Z]+.*"
    let texttest = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx)
    guard texttest.evaluate(with: password) else { return false }

    let numberRegEx  = ".*[0-9]+.*"
    let texttest1 = NSPredicate(format:"SELF MATCHES %@", numberRegEx)
    guard texttest1.evaluate(with: password) else { return false }

    let specialCharacterRegEx  = ".*[!&^%$#@()/_*+-]+.*"
    let texttest2 = NSPredicate(format:"SELF MATCHES %@", specialCharacterRegEx)
    guard texttest2.evaluate(with: password) else { return false }

    return true
}

答案 2 :(得分:3)

另一种替代解决方案。

您可以使用一个正则表达式执行所有检查。

RegExp: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]{8,}

您可以在下面提到这一点:

//Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character:
let regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]{8,}"
let isMatched = NSPredicate(format:"SELF MATCHES %@", regex).evaluate(with: yourTextField.text)
if(isMatched  == true) {
    // Do your stuff ..
}  else {
    // Show Error Message.
}

答案 3 :(得分:0)

至少1个大写字母,1个小写字母,1个特殊字符,1个数字和总共8个字符。

要支持列出的所有特殊字符 https://www.owasp.org/index.php/Password_special_characters

!“ ##%&'()* +,-。/ :; <=>?@ [] ^ _`{|}〜

extension String {
    func isValidPassword() -> Bool {
        //let passwordRegex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[ !\"\\\\#$%&'()*+,-./:;<=>?@\\[\\]^_`{|}~])[A-Za-z\\d !\"\\\\#$%&'()*+,-./:;<=>?@\\[\\]^_`{|}~]{8,}"
        //safe to escape all regex chars
        let passwordRegex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[ !\"\\\\#$%&'\\(\\)\\*+,\\-\\./:;<=>?@\\[\\]^_`\\{|\\}~])[A-Za-z\\d !\"\\\\#$%&'\\(\\)\\*+,\\-\\./:;<=>?@\\[\\]^_`\\{|\\}~]{8,}"
        return NSPredicate(format: "SELF MATCHES %@", passwordRegex).evaluate(with: self)
    }
}