所以从技术上来说这不是重复的帖子。我试图确定为什么我不能让我的拆分正则表达式来验证。我决定将它拆分,以便更具可读性。它曾经有过一段时间,但由于某些原因,我似乎无法让它合作。我不小心添加了一些我不应该添加的东西吗?正则表达式开头的额外(|)是允许空的IPv6地址。
这是代码:(是的,这是一个函数,我只是没有包括其余部分。)
local IPV6="^(|([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6}|"
"([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}|"
"([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}|"
"([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}|"
"([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}|"
"([0-9a-f]{1,4}:){1,6}(:[0-9a-f]{1,4}){1,1}|"
"(([0-9a-f]{1,4}:){1,7}|:):|"
":(:[0-9a-f]{1,4}){1,7}|"
"((([0-9a-f]{1,4}:){6})(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})|"
"(([0-9a-f]{1,4}:){5}[0-9a-f]{1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})|"
"([0-9a-f]{1,4}:){5}:[0-9a-f]{1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}|"
"([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}|"
"([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,3}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}|"
"([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,2}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}|"
"([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,1}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}|"
"(([0-9a-f]{1,4}:){1,5}|:):(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}|"
":(:[0-9a-f]{1,4}){1,5}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})$"
if [[ !( $ADDRESS =~ $IPV6 ) ]]; then
echo "OOPS"
return
fi
答案 0 :(得分:1)
似乎简写\d
在bash中不起作用。以下适用于我:
R10='([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}'
R11='([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6}'
R12='([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}'
R13='([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}'
R14='([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}'
R15='([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}'
R16='([0-9a-f]{1,4}:){1,6}(:[0-9a-f]{1,4}){1,1}'
R17='(([0-9a-f]{1,4}:){1,7}|:):'
R18=':(:[0-9a-f]{1,4}){1,7}'
R0='(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])){3}'
R1='(([0-9a-f]{1,4}:){6})'
R2='([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,4}:'
R3='([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,3}:'
R4='([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,2}:'
R5='([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,1}:'
R6='(([0-9a-f]{1,4}:){1,5}|:):'
R7=':(:[0-9a-f]{1,4}){1,5}:'
local IPV6="^(${R10}|${R11}|${R12}|${R13}|${R14}|${R15}|${R16}|${R17}|${R18}|${R19}|${R1}${R0}|${R2}${R0}|${R3}${R0}|${R4}${R0}|${R5}${R0}|${R6}${R0}|${R7}${R0})\$"
if [[ !( $ADDRESS =~ $IPV6 ) ]]; then
echo "OOPS"
return
fi