Skip to content

Instantly share code, notes, and snippets.

@shankara-subramani
Last active March 19, 2021 02:33
Show Gist options
  • Save shankara-subramani/0cb36d9cbf72525365e975cc94097481 to your computer and use it in GitHub Desktop.
Save shankara-subramani/0cb36d9cbf72525365e975cc94097481 to your computer and use it in GitHub Desktop.
password regex with optional special characters
^(?=.*\d)(?=.*[a-z])[\w~@#$%^&*+=`|{}:;!.?\"()\[\]-]{6,8}$
  • ^ - start of string
  • (?=.*\d) - require at least one digit
  • (?=.*[a-z]) - require at least a lowercase ASCII letter
  • (?=.*[A-Z]) - require at least 1 ASCII uppercase letter
  • [\w~@#$%^&*+=`|{}:;!.?\"()\[\]-]{6,8} - match (=consume) 6,8 characters only,symbols that are either alphanumeric, or _, or all those inside the character class
  • $ - end of string

For testing/demo go here: https://regex101.com/r/qN5dN0/5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment