how to Allow ! but block consecutive !! in regular expression in shell scripting in the below code

问题内容:

#!/bin/bash

default_pwd="mysql_password"
script_pwd="template!123"
echo $default_pwd
echo $script_pwd
if [ $default_pwd == $script_pwd ]
then
    echo "They match"
else
    if [ `expr "$script_pwd" : ".*[!]{2}$.*"` -gt 0];
    then
            echo "Contain consecutive !! special character"
    else
            echo "valid password"
    fi
fi

Expected Result:
it should echo “consecutive !! special character” when script_pwd has !!

问题评论:

    
Beware that $ looks for an end of string. Then your regex looks like: match 2 consecutive occurences of ! immediately before end of string. If you want a match anywhere in the string you could use only !+.

答案:

答案1:

Use =~ operator to match for regular expressions.

default_pwd="mysql_password"
script_pwd="test!!123"
echo $default_pwd
echo $script_pwd
if [ $default_pwd == $script_pwd ]
then
    echo "They match"
else
    re='.*[!]{0,2}.*'
    if [[ $script_pwd =~ $re ]];
    then
            echo "Contain consecutive !! special character"
    else
            echo "valid password"
    fi
fi

答案评论:

原文地址:

https://stackoverflow.com/questions/47751653/how-to-allow-but-block-consecutive-in-regular-expression-in-shell-scripting

Tags:,

添加评论

友情链接:蝴蝶教程