且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-22 20:30:51

只需替换您的RegEx规则[AZ] + with。* [AZ] +。*(以及其他RegEx规则)

Simply replace your RegEx rule [A-Z]+ with .*[A-Z]+.* (and other RegEx rules as well)

规则


[AZ] +仅匹配所有字符大写的字符串

[A-Z]+ matches only strings with all characters capitalized

示例:AVATAR,AVA,TAR,AAAAAA

无效:AVATAr


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

.* matches all strings (0+ characters)

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


。* [AZ] +。*匹配所有至少有一个资本的字符串信

.*[A-Z]+.* matches all strings with at least one capital letter

示例:Avatar,avataR,aVatar

说明:

我。 。*将尝试匹配0或更多的任何东西

II。 [A-Z] +将需要至少一个大写字母(因为+)

III。 。*将尝试匹配0或更多的任何东西

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]

Avatar [empty | "A" | "vatar"]
aVatar ["a" | "V" | "atar"]
aVAtar ["a" | "VA" | "tar"]

工作代码

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