如何使用shell脚本中的正则表达式验证给定的输入字符串是否有效十六进制值
例如:
Input var="ff:ff:fe:ff"
有:
分隔符值
我想将此正则表达式用于任何输入String
var = "ff:ff:fe:ff:fe"
var = "ff:ff:fe:ff:fe:fe:ff:ff"
\b0[xX][0-9a-fA-F]+\b
#!/bin/bash -x
var="fe:fe:fe:fe"
regex="/^([0-9A-F]+:?){4}$/"
if [[ $var =~ $regex ]]; then
echo "valid"
fi
答案 0 :(得分:3)
^([0-9A-Fa-f]{2})(:[0-9A-Fa-f]{2})*$
测试
$ if [[ "ff:af:ff:23:a2:ad" =~ ^([0-9A-Fa-f]{2})(:[0-9A-Fa-f]{2})*$ ]]; then
echo "match";
fi
$ match
$ if [[ "definitlynottherightformat" =~ ^([0-9A-Fa-f]{2})(:[0-9A-Fa-f]{2})*$ ]]; then
echo "match";
fi
$