I'd like to use regular expression to validate the characters requirement of a password.
Requirement: Password should have 16 characters.
I've tried to use regular expression with a positive lookahead but it does not work finally:
echo 'XXXX9999ccccXXX%' | grep -P '^((?=.*[0-9]).{4})((?=.*[a-z]).{4})((?=.*[A-Z]).{4})((?=.*\pP).{4})$'
Your lookahead syntax is off, because it is not correctly checking the positions you mentioned in your requirements. The following regex pattern seems to work for me:
^(?=.{0,3}\d)(?=.{4,7}[a-z])(?=.{8,11}[A-Z])(?=.{12,15}[.,$%^&!@]).{16}$
Explanation:
(?=.{0,3}\d) - number in positions 1-4
(?=.{4,7}[a-z]) - lowercase in positions 5-8
(?=.{8,11}[A-Z]) - uppercase in positions 9-12
(?=.{12,15}[.,$%^&!@]) - symbol in positions 13-16
I don't know grep or Linux well enough to comment on whether you are making best use, but this should at least fix any problems you were having with the pattern.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With