RegEx No more than 2 identical consecutive characters and a-Z and 0-9

前端 未结 1 528
醉话见心
醉话见心 2020-11-28 14:08

Edit: Thanks for the advice to make my question clearer :)

The Match is looking for 3 consecutive characters:

Regex Match

相关标签:
1条回答
  • 2020-11-28 15:07

    http://regexr.com?34vo9

    The regular expression:

    ^(?=.{8,20}$)(([a-z0-9])\2?(?!\2))+$
    

    The first lookahead ((?=.{8,20}$)) checks the length of your string. The second portion does your double character and validity checking by:

    (
      ([a-z0-9])      Matching a character and storing it in a back reference.
      \2?             Optionally match one more EXACT COPY of that character.
      (?!\2)          Make sure the upcoming character is NOT the same character.
    )+                Do this ad nauseum.
    $                 End of string.
    

    Okay. I see you've added some additional requirements. My basic forumla still works, but we have to give you more of a step by step approach. SO:

    ^...$
    

    Your whole regular expression will be dropped into start and end characters, for obvious reasons.

    (?=.{n,m}$)
    

    Length checking. Put this at the beginning of your regular expression with n as your minimum length and m as your maximum length.

    (?=(?:[^REQ]*[REQ]){n,m})
    

    Required characters. Place this at the beginning of your regular expression with REQ as your required character to require N to M of your character. YOu may drop the (?: ..){n,m} to require just one of that character.

    (?:([VALID])\1?(?!\1))+
    

    The rest of your expression. Replace VALID with your valid Characters. So, your Password Regex is:

    ^(?=.{8,20}$)(?=[^A-Za-z]*[A-Za-z])(?=[^0-9]*[0-9])(?:([\w\d*?!:;])\1?(?!\1))+$
    

    'Splained:

    ^
      (?=.{8,20}$)                 8 to 20 characters
      (?=[^A-Za-z]*[A-Za-z])       At least one Alpha
      (?=[^0-9]*[0-9])             At least one Numeric
      (?:([\w\d*?!:;])\1?(?!\1))+  Valid Characters, not repeated thrice.
    $
    

    http://regexr.com?34vol Here's the new one in action.

    0 讨论(0)
提交回复
热议问题