0
[a-zA-Z0-9\@\#\$\%\&\*\(\)\-\_\+\]\[\'\;\:\?\.\,\!\^]+$ 

有効な出力は次のとおりです。 reahb543)(*&&!@#$%^kshABmhbahdxb!@$@#%6813741646

これは私が持っている表現です。しかし、値は8 ~ 32 桁にする必要があります。

したがって、有効な文字列は次のようになります。

  • 8 ~ 32 文字
  • 数字、アルファベット、特殊文字を含む
4

1 に答える 1

4

説明

あなたの表現で私が変更したいことがいくつかあります:

  • 文字クラス内のすべての文字のエスケープは不要です
  • この文字は文字クラス内で特別な意味を持ち、クラスの最後または最初にある必要があるため、文字クラス内のダッシュを最後に移動します
  • 先読みを追加して、文字列に必要な桁数を強制します
  • 開始文字列アンカーを追加して、許可されているよりも多くの数字を含む可能性のある長い文字列に文字列が一致しないようにする必要があります

この式は次のようになります。

  • 文字列は 8 ~ 32 桁である必要があり、それ以上またはそれ以下は許可されません
  • 文字セットから任意の数の文字を許可します (ここでの他のルールも真である場合)
  • 文字列の最初または最後の文字として数字を表示できるようにする

^(?=(?:\D*?\d){8,32}(?!.*?\d))[a-zA-Z0-9@\#$%&*()_+\]\[';:?.,!^-]+$

ここに画像の説明を入力

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (between 8 and
                             32 times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      \D*?                     non-digits (all but 0-9) (0 or more
                               times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      \d                       digits (0-9)
--------------------------------------------------------------------------------
    ){8,32}                  end of grouping
--------------------------------------------------------------------------------
    (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
      .*?                      any character except \n (0 or more
                               times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      \d                       digits (0-9)
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [a-zA-Z0-                any character of: 'a' to 'z', 'A' to 'Z',
  9@\#$%&*()_+\]\[';:?     '0' to '9', '@', '\#', '$', '%', '&', '*',
  .,!^-]+                  '(', ')', '_', '+', '\]', '\[', ''', ';',
                           ':', '?', '.', ',', '!', '^', '-' (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

ライブデモ

サンプル

reahb)(*&&!@#$%^kshABmhbahdxb!@$@#%1234567   = bad
reahb)(*&&!@#$%^kshABmhbahdxb!@$@#%12345678  = good
1234reahb)(*&&!@#$%^kshABmhbahdxb!@$@#%5678  = good
1234reahb)(*&&!@#$%^kshABmhbahdxb!@$@#%5678901234567890123456789012   = good
1234reahb)(*&&!@#$%^kshABmhbahdxb!@$@#%56789012345678901234567890123  = bad
reahb)(*&&!@12345678901234567890123456789012#$%^kshABmhbahdxb!@$@#%   = good
reahb)(*&&!@123456789012345678901234567890123#$%^kshABmhbahdxb!@$@#%  = bad



または

文字クラスから任意のタイプの 8 ~ 32 文字のみを許可する場合は、次のように動作します。

^[a-zA-Z0-9@\#$%&*()_+\]\[';:?.,!^-]{8,32}$

ここに画像の説明を入力

于 2013-08-28T01:01:13.810 に答える