Regular expression for no more than two repeated letters/digits

前端 未结 5 1711
北恋
北恋 2020-12-03 07:23

I have a requirement to handle a regular expression for no more than two of the same letters/digits in an XSL file.

  • no space
  • does not support special
5条回答
  •  没有蜡笔的小新
    2020-12-03 07:50

    This regex will do it: ^(?!.*([A-Za-z0-9])\1{2})(?=.*[a-z])(?=.*\d)[A-Za-z0-9]+$

    Here's the breakdown:

    (?!.*([A-Za-z0-9])\1{2}) makes sure that none of the chars repeat more than twice in a row.

    (?=.*[a-z]) requires at least one lowercase letter

    (?=.*\d) requires at least one digit

    [A-Za-z0-9]+ allows only letters and digits

    EDIT : removed an extraneous .* from the negative lookahead

提交回复
热议问题