I have the following regex that checks for a password policy. it is validated to work:
(^([zZ]\d{3})*$)|((?=.{9,})(?=.*?[^\w\s])(?=.*?[0-9])(?=.*?[A-Z]).*?[a-z].*)
I want to use the regex in a bash script to validate a psssword the following way :
echo $password | grep "(^([zZ]\d{3})*$)|((?=.{9,})(?=.*?[^\w\s])(?=.*?[0-9])(?=.*?[A-Z]).*?[a-z].*)"
if [[ $? -eq 0 ]] ; then
This does not work in bash.
My question is :
How do i convert this “pure” regex to a one that work in bash ? which characters do i need to escape, what is the correct way to pass the regex to grep ? are there any other things i need to look out for ?
Thanks
This may be difficult.
Standard
grephas limited capabilities. It only supports POSIX extended regular expressions which do not recognize the lookahead assertions your regex relies on.If you have GNU
grepon your machine, you can pass it the-Por--perl-regexpparameter, allowing it to use Perl compatible regular expressions. Then your regex should work.As mentioned in my comment, the regex as-is is not suitable for passwort validation. It allows passwords like
z000or even the empty string:Better use