1

次の正規表現を使用してパスワードを照合する際に問題があります。

^[A-Za-z\d[\!\@\#\$\%\^\&\*\(\)\_\+]{1,}]{6,}$

上記の式では、残りの文字は英数字である必要がある場所に少なくとも 1 つの特殊文字を入力する必要があります。パスワードの長さは 6 文字未満にすることはできません。

ただし、上記の式では、ユーザーは特殊文字を入力できません。ユーザーが少なくとも1つの特殊文字を入力するように制限するにはどうすればよいですか?

4

2 に答える 2

9

どうですか:

^(?=[\w!@#$%^&*()+]{6,})(?:.*[!@#$%^&*()+]+.*)$

説明:

The regular expression:

(?-imsx:^(?=[\w!@#0^&*()+]{6,})(?:.*[!@#0^&*()+]+.*)$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    [\w!@#0^&*()+]{6,}       any character of: word characters (a-z,
                             A-Z, 0-9, _), '!', '@', '#', '0', '^',
                             '&', '*', '(', ')', '+' (at least 6
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
    [!@#0^&*()+]+            any character of: '!', '@', '#', '0',
                             '^', '&', '*', '(', ')', '+' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2013-05-16T08:29:41.473 に答える
3

正規表現を複雑にする代わりに、文字を繰り返し処理して特別なものを数えるのはどうですか

count = 0
for char in string:
    if isspecial(char):
        count = count+1

if count > 1:
    reject()
于 2013-05-16T06:32:01.713 に答える