Regular Expressions 101

Community Patterns

password with at least 1 special character

1

Regular Expression
PCRE (PHP <7.3)

/
^(\w{7,}[!-\/:-@[-`{-~]|(\w{6}[!-\/:-@[-`{-~]|(\w{5}[!-\/:-@[-`{-~]|(\w{4}[!-\/:-@[-`{-~]|(\w{3}[!-\/:-@[-`{-~]|(\w\w[!-\/:-@[-`{-~]|(\w[!-\/:-@[-`{-~]|[!-\/:-@[-`{-~]([!-~]))([!-~]))([!-~]))([!-~]))([!-~]))([!-~]))([!-~]))([!-~])*$
/
mg

Description

This regex is to describe the password which has at least 7 characters (OC) with at least a special one (SC). Of course, we know the SC may be in the first, second .... or last of the password. Also, we know the password maybe bigger than just 8. so I wrote the RE to accept any character (AC) from ! to ~ which Contains all characters in the end with * {0, }. but the problem in the beginning before writing 8 characters.

  • The user may enter 7 AC in the first then SC.
  • Or 6 AC then SC then at least 1 AC.
  • Or 5 AC then SC then at least 2 AC.
  • and so on

special characters are : https://tools.oratory.com/altcodes.html ! "

$ % & ' ( ) * + ,

. / : ; <

? @ [
] ^ _ ` { | } ~

Submitted by EMAM1999 - 3 years ago