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

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

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

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

Ответ 2

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

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

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

^                         Start anchor
(?=.*[A-Z].*[A-Z])        Ensure string has two uppercase letters.
(?=.*[[email protected]#$&*])            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.

Источник - Рублар Ссылка

Ответ 3

Попробуйте с этим, потому что Пароль должен быть более 6 символов, по крайней мере, один заглавный, цифровой или специальный символ

^.*(?=.{6,})(?=.*[AZ])(?=.*[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)(?=.*[[email protected]$!%*#?&])[A-Za-z\\[email protected]$!%*#?&]{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)(?=.*[[email protected]$!%*?&#])[A-Za-z\\[email protected]$!%*?&#]{8,}"

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

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

Ответ 4

public func isValidPassword() -> Bool {
    let passwordRegex = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[[email protected]#$%^&*()\\-_=+{}|?>.<,:;~']{8,}$"
    return NSPredicate(format: "SELF MATCHES %@", passwordRegex).evaluate(with: self)
}

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