字母数字和特殊字符RegEx 8-32位

时间:2013-08-27 17:21:05

标签: java regex alphanumeric

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

有效的输出是:reahb543)(*&&!@#$%^kshABmhbahdxb!@$@#%6813741646

这是我的表达方式。但我需要的值仅为 8到32位

所以有效的字符串是:

  • 8到32个字符
  • 包括数字,字母,特殊字符

1 个答案:

答案 0 :(得分:4)

描述

我会在你的表达中改变一些事情:

  • 字符类中所有字符的转义都是不必要的
  • 将字符类中的破折号移动到最后,因为此字符在字符类中确实具有特殊含义,并且需要位于类的结尾或开头
  • 添加前瞻以强制字符串中所需的位数
  • 添加一个起始字符串锚点,要求阻止字符串匹配可能包含更多数字然后允许的更长字符串

此表达式将:

  • 要求字符串包含8到32位数字,不允许更多或更少
  • 允许字符集中的任意数量的字符(此处提供其他规则也是如此)
  • 允许数字显示为字符串的第一个或最后一个字符

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

enter image description here

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

实施例

Live Demo

<强>样品

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

enter image description here