【发布时间】:2013-12-30 18:44:47
【问题描述】:
我在 Bash Shell 脚本中使用正则表达式。我正在使用下面的正则表达式代码来检查密码标准:密码至少应包含 6 个字符,至少包含一位数字和至少一个大写字母。我在正则表达式验证工具中进行了验证,我形成的正则表达式工作正常。但是,它在 Bash Shell 脚本中失败了。请提供您的想法。
echo "Please enter password for User to be created in OIM: "
echo "******Please Note: Password should be at least 6 characters long with one digit and one Upper case Alphabet******"
read user_passwd
regex="^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)\S{6,}$"
echo $user_passwd
echo $regex
if [[ $user_passwd =~ $regex ]]; then
echolog "Password Matches the criteria"
else
echo "Password criteria: Password should be at least 6 characters long with one digit and one Upper case Alphabet"
echo "Password does not Match the criteria, exiting..."
exit
fi
【问题讨论】:
-
单个正则表达式不一定是此类问题的最佳解决方案。从anubhava's answer可以看出,一系列直接对应你需求的测试可以更简单,更容易维护。
-
您的正则表达式检查至少一个小写字母,但您没有在问题中提及该要求。
标签: bash