正则表达式不匹配,即使匹配 bash 中的确切值

Regex is not matching even though match exact value in bash

echo "Hello World";
string="v13.2.exe"
pattern='^v[0-9]*\.[0-9]*\.exe$'

if  [[ $str =~ pattern ]]; then
    echo "found"
else
    echo "not found"
fi

总是打印找不到。怎么了

在一种情况下 ($str) 您没有使用您定义的变量 ($string)。在另一个 (pattern) 中,您缺少 $ 符号 ($pattern)。尝试

string="v13.2.exe"
pattern='^v[0-9]*\.[0-9]*\.exe$'

if  [[ $string =~ $pattern ]]; then
    echo "found"
else
    echo "not found"
fi
found