Как реализовать регулярное выражение для проверки пароля в Swift?

Я хочу реализовать regex validaton для паролей в Swift? Я пробовал следующее регулярное выражение, но не успешно

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

мое требование заключается в следующем: пароль должен быть более 6 символов, по крайней мере, с одним заглавным, числовым или специальным символом

4 ответов


Вы можете использовать Regex для проверки вашей проверки вашего пароль сила

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

Regex Объяснение: -

^                         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.

источник - Rublar Ссылке


попробуйте с этим для пароль должен быть более 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

https://regex101.com/#javascript

больше вы можете попробовать ....

минимум 8 символов по крайней мере 1 алфавит и 1 число:

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

минимум 8 символов по крайней мере 1 алфавит, 1 число и 1 специальный Характер:

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

минимум 8 символов по крайней мере 1 заглавный алфавит, 1 строчный алфавит и 1 число:

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

минимум 8 символов по крайней мере 1 заглавный алфавит, 1 строчный алфавит, 1 число и 1 специальный символ:

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

минимум 8 и максимум 10 символов по крайней мере 1 заглавный алфавит, 1 строчный алфавит, 1 число и 1 специальный символ:

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

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)
}

Если вам нужно быстро исправить. Это проверка пароля с помощью regex. Скопируйте / вставьте файл helper или extension и используйте его.


регулярное выражение

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