Regular expression for passphrase

I am new to regex and trying to create a regex to check passwords. I would like to confirm that the phrase has:

  • n number of words or more
  • words must be separated by spaces
  • words each containing n characters or more
  • number in at least one word
  • at least one special character in one of the words

That's what i still have

^(?=.*?((?:.*?\b[a-zA-Z0-9]{2,40}\b)\s*){3,})(?=.*?[@#$%^&+=])(?=.*?[0-9]).*$

It matches this phrase Pe2sI#sHy?ThYulU#, which does not have at least 3 words (no spaces). What am I doing wrong?

+3
source share
2 answers

\s+ \s*. , . . :

^                 # Start of string
(?=.*\d)          # Assert at least one digit
(?=.*[@#$%^&+=])  # Assert at least one special char
\s*               # Optional leading whitespace
(?:               # Match...
 \S{2,}           #  at least 2 non-spaces
 \s+              #  at least 1 whitespace
){2,}             # at least 2 times
\S{2,}            # Match at least 2 non-spaces (making 3 "words" minimum)
+3

, .

@Tim Pietzcker.
"" , 3 [a-zA-Z0-9] {2,40} , .

^                      # String start
(?=.*[@#$%^&+=])       # Assert 1 special char
(?=.*\d)               # Assert 1 digit
(?:                    # Special 'Word Group'  -- Need 2 words
   .*                     # Any char, 0 or more times
   [a-zA-Z0-9]{2,40}      # Alpha/num char, 2 to 40 times
   .*                     # Any char, 0 or more times
   \s                     # a whitespace, only 1 required
){2}                   # 'Word Group' end, do 2 times
.*                     # Any char, 0 or more times
[a-zA-Z0-9]{2,40}      # Alpha/num char, 2 to 40 times -- Need 1 word

3 [a-zA-Z0-9] {2,40} , 1
.

, , {3} , , .

^(?=.*[@#$%^&+=])(?=.*\d)(?:(?:(?!\1)|\s).*[a-zA-Z0-9]{2,40}().*){3} 
                                                       ^          ^
---------------------------------------   

^                      # String start
(?=.*[@#$%^&+=])       # Assert 1 special char
(?=.*\d)               # Assert 1 digit
(?:                    # Special 'Word Group'
   (?:                    #.. grping start ....
        (?!\1)              # Either capt group 1 is UN-DEFINED
      | \s                  # OR, require a whitespace
    )                     #.. grping end ....
   .*                     # Any char, 0 or more times
   [a-zA-Z0-9]{2,40}      # Alpha/num char, 2 to 40 times
   ()                     # DEFINE Capture group 1
   .*                     # Any char, 0 or more times
){3}                   # 'Word Group' end, do 3 times

,

^(?=.*[@#$%^&+=])(?=.*\d)(?:(?(1)\s).*([a-zA-Z0-9]{2,40}).*){3}
                                                   ^         ^
---------------------------------------   

^                      # String start
(?=.*[@#$%^&+=])       # Assert 1 special char
(?=.*\d)               # Assert 1 digit
(?:                    # Special 'Word Group'
   (?(1)\s)               # Conditional, require a whitespace if capture group 1 captured anything
   .*                     # Any char, 0 or more times
   ([a-zA-Z0-9]{2,40})    # Capture group 1, Alpha/num char, 2 to 40 times
   .*                     # Any char, 0 or more times
){3}                   # 'Word Group' end, do 3 times
+1

All Articles