0

I want to write a regular expression for a password with these conditions:

1- The password must contains 4 letters at least.

2- The password must contains 2 digits.

3- The password must contains 2 punctuation symbols.

4- The password must have a minimum length of 8 characters.

This is the regular expresion that I have: "^((?=.*?\\p{L}.{4,})(?=.*?\\p{N}.{2,})(?=.*?\\p{P}.{2,})).{8,}$"

And my problem is that yhis expresion doesn't accept this as password an I don't know why: "abcd12.-".

Can anyone help me with this problem? Thank you

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • You could use the quantifier for the groups instead `^(?=(?:.*\p{L}){4})(?=(?:.*\p{N}){2,})(?=(?:.*\p{P}){2,}).{8,}$` https://regex101.com/r/0MEJ74/1 – The fourth bird Jan 04 '20 at 16:49

1 Answers1

0

This part in the expressions (?=.*?\\p{L}.{4,} checks if there is a letter followed by 4 or more times any character.

You could update your expression to use non capturing groups (?: and repeat the groups n times matching a letter, a number or punctuation instead.

 ^(?=(?:.*\\p{L}){4})(?=(?:.*\\p{N}){2})(?=(?:.*\\p{P}){2}).{8,}$

Regex demo

Note that using .{8,} matches at least 8 times any character except a newline, also matching spaces.

The fourth bird
  • 96,715
  • 14
  • 35
  • 52