my regex code not working perfectly need assistance

Viewed 33

i am trying to remove all lines that its password doesnt follow my standard policy username:password the policy are

Passwords are after ":" And password policy is that the password must be 7 to 32 characters long and The password must contain a mix of letters, numbers, and/or special characters also passwords containing only letters or only numbers are not accepted
username:Password42
Username52@:sssdt3
user:Pass!626795
use:uss

removing all lines only leaving

username:Password42
user:Pass!626795

i tried using regex, Made this one but it doesnt work perfectly, Why so? Anyone can fix it?

^:*(?!(?=.*\d)(?=.*[a-z])(?=.*[A-Z])|(?=.*\d)(?=.*[a-z])(?=.*[!@#$%^&*()_+])|(?=.*\d)(?=.*[A-Z])(?=.*[!@#$%^&*()_+])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+])).*$\R*
1 Answers

The pattern you can consider using is

^[^:\r\n]+:(?!(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{7,32}$).*\R?

See the regex demo

Details:

  • ^ - start of a line
  • [^:\r\n]+ - 1 or more chars other than a colon, CR and LF
  • : - a colon
  • (?!(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{7,32}$) - negative lookahead that fails the match if there are seven to thirty-two chars other than line break chars immediately to the right of the current location, that contain both a digit, lower- or uppercase letter
  • .* - any 0 or more chars other than line break chars (the rest of the line)
  • \R? - an optional line break sequence

NOTE you may add more alternatives to the negative lookahead. E.g. replace (?=.*\d)(?=.*[a-zA-Z]) with (?:(?=.*\d)(?=.*[a-z])(?=.*[A-Z])|(?=.*\d)(?=.*[!@#$%^&*()_+])) to also require digits and special chars of your choice in the pattern to negate.

Related