因此,这不是技术上的重复帖子。我试图确定为什么我无法获得分裂的正则验证。我决定将其分开,以便更可读。它在某一时刻起作用,但是现在出于某种原因,我似乎无法合作。我是否意外地添加了我不应该有的东西?正则一开始的额外(|)是允许一个空的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
似乎速记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