regex repeated character count

前端 未结 2 781
时光取名叫无心
时光取名叫无心 2021-01-01 07:18

If I have a set of characters like \"abcdefghij\" and using this characters, I generate random a password using this characters. A generated password can have, for example,

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 08:07

    You could use something like:

    /^
      (?:(.)
         (?!\1)           # neighbor characters are not identical
         (?!(?>.*?\1){2}) # character does not occur more than twice
      )*
    \z/x
    

    Perl quoting, the atomic group can be removed if not supported.


    In Java regex it could be written like:

    ^(?:(.)(?!\1|(?:.*?\1){2}))*\z
    

提交回复
热议问题