如何在Swift中实现密码验证的正则表达式?

Vai*_*eri 33 regex validation ios swift

我想在Swift中为密码实现正则表达式验证?我试过以下正则表达式,但没有成功

([(0-9)(A-Z)(!@#$%ˆ&*+-=<>)]+)([a-z]*){6,15}
Run Code Online (Sandbox Code Playgroud)

我的要求如下:密码必须超过6个字符,至少有一个大写字母,数字或特殊字符

Ana*_*mje 63

您可以使用Regex检查密码强度

^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$
Run Code Online (Sandbox Code Playgroud)

正则表达式说明: -

^                         Start anchor
(?=.*[A-Z].*[A-Z])        Ensure string has two uppercase letters.
(?=.*[!@#$&*])            Ensure string has one special case letter.
(?=.*[0-9].*[0-9])        Ensure string has two digits.
(?=.*[a-z].*[a-z].*[a-z]) Ensure string has three lowercase letters.
.{8}                      Ensure string is of length 8.
$                         End anchor.
Run Code Online (Sandbox Code Playgroud)

来源 - Rublar Link

  • 该解释有助于更好地理解并因此创建所需的正则表达式。 (2认同)

Naz*_*san 42

试试这个密码必须超过6个字符,至少有一个大写字母,数字或特殊字符

^.*(?=.{6,})(?=.*[A-Z])(?=.*[a-zA-Z])(?=.*\\d)|(?=.*[!#$%&? "]).*$

^ assert position at start of the string
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
(?=.{6,}) Positive Lookahead - Assert that the regex below can be matched
.{6,} matches any character (except newline)
Quantifier: {6,} Between 6 and unlimited times, as many times as possible, giving back as needed [greedy]
(?=.*[A-Z]) Positive Lookahead - Assert that the regex below can be matched
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
[A-Z] match a single character present in the list below
A-Z a single character in the range between A and Z (case sensitive)
(?=.*[a-zA-Z]) Positive Lookahead - Assert that the regex below can be matched
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
[a-zA-Z] match a single character present in the list below
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
(?=.*\\d) Positive Lookahead - Assert that the regex below can be matched
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\d match a digit [0-9]
2nd Alternative: (?=.*[!#$%&? "]).*$
(?=.*[!#$%&? "]) Positive Lookahead - Assert that the regex below can be matched
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
[!#$%&? "] match a single character present in the list below
!#$%&? " a single character in the list !#$%&? " literally (case sensitive)
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
$ assert position at end of the string
Run Code Online (Sandbox Code Playgroud)

https://regex101.com/#javascript

更多你可以尝试....

至少8个字符,至少1个字母和1个数字:

"^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$"
Run Code Online (Sandbox Code Playgroud)

至少8个字符,至少1个字母,1个数字和1个特殊字符:

"^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,}$"
Run Code Online (Sandbox Code Playgroud)

至少8个字符,至少1个大写字母,1个小写字母和1个数字:

"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,}$"
Run Code Online (Sandbox Code Playgroud)

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

"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[d$@$!%*?&#])[A-Za-z\\dd$@$!%*?&#]{8,}"
Run Code Online (Sandbox Code Playgroud)

最少8个字符和最多10个字符至少1个大写字母,1个小写字母,1个数字和1个特殊字符:

"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&#])[A-Za-z\\d$@$!%*?&#]{8,10}"
Run Code Online (Sandbox Code Playgroud)


tBu*_*Bug 16

public func isValidPassword() -> Bool {
    let passwordRegex = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z!@#$%^&*()\\-_=+{}|?>.<,:;~`’]{8,}$"
    return NSPredicate(format: "SELF MATCHES %@", passwordRegex).evaluate(with: self)
}
Run Code Online (Sandbox Code Playgroud)

如果你需要快速修复.这是使用正则表达式验证密码.复制/粘贴助手或扩展文件并使用它.


Yuy*_*tsu 11

func isValidPassword() -> Bool {
    // least one uppercase,
    // least one digit
    // least one lowercase
    // least one symbol
    //  min 8 characters total
    let password = self.trimmingCharacters(in: CharacterSet.whitespaces)
    let passwordRegx = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&<>*~:`-]).{8,}$"
    let passwordCheck = NSPredicate(format: "SELF MATCHES %@",passwordRegx)
    return passwordCheck.evaluate(with: password)

}
Run Code Online (Sandbox Code Playgroud)

缺少验证:

func getMissingValidation(str: String) -> [String] {
    var errors: [String] = []
    if(!NSPredicate(format:"SELF MATCHES %@", ".*[A-Z]+.*").evaluate(with: str)){
        errors.append("least one uppercase")
    }
    
    if(!NSPredicate(format:"SELF MATCHES %@", ".*[0-9]+.*").evaluate(with: str)){
        errors.append("least one digit")
    }

    if(!NSPredicate(format:"SELF MATCHES %@", ".*[!&^%$#@()/]+.*").evaluate(with: str)){
        errors.append("least one symbol")
    }
    
    if(!NSPredicate(format:"SELF MATCHES %@", ".*[a-z]+.*").evaluate(with: str)){
        errors.append("least one lowercase")
    }
    
    if(str.count < 8){
        errors.append("min 8 characters total")
    }
    return errors
}
Run Code Online (Sandbox Code Playgroud)


Vai*_*eri 8

正则表达式是

(?:(?:(?=.*?[0-9])(?=.*?[-!@#$%&*ˆ+=_])|(?:(?=.*?[0-9])|(?=.*?[A-Z])|(?=.*?[-!@#$%&*ˆ+=_])))|(?=.*?[a-z])(?=.*?[0-9])(?=.*?[-!@#$%&*ˆ+=_]))[A-Za-z0-9-!@#$%&*ˆ+=_]{6,15}
Run Code Online (Sandbox Code Playgroud)