我正在尝试编写一个正则表达式,用于接受从8000::
到ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
的IPv6子网。子网基于/1 - 128
之间的前缀范围
IPv6的有效网络掩码示例列表:
The prefix /1 is equal to netmask 8000::
The prefix /2 is equal to netmask c000::
The prefix /3 is equal to netmask e000::
The prefix /4 is equal to netmask f000::
The prefix /5 is equal to netmask f800::
The prefix /6 is equal to netmask fc00::
The prefix /7 is equal to netmask fe00::
The prefix /8 is equal to netmask ff00::
The prefix /9 is equal to netmask ff80::
The prefix /10 is equal to netmask ffc0::
The prefix /11 is equal to netmask ffe0::
The prefix /12 is equal to netmask fff0::
The prefix /13 is equal to netmask fff8::
The prefix /14 is equal to netmask fffc::
The prefix /15 is equal to netmask fffe::
The prefix /16 is equal to netmask ffff::
The prefix /17 is equal to netmask ffff:8000::
The prefix /18 is equal to netmask ffff:c000::
The prefix /19 is equal to netmask ffff:e000::
The prefix /20 is equal to netmask ffff:f000::
The prefix /21 is equal to netmask ffff:f800::
The prefix /22 is equal to netmask ffff:fc00::
The prefix /23 is equal to netmask ffff:fe00::
The prefix /24 is equal to netmask ffff:ff00::
The prefix /25 is equal to netmask ffff:ff80::
The prefix /26 is equal to netmask ffff:ffc0::
The prefix /27 is equal to netmask ffff:ffe0::
The prefix /28 is equal to netmask ffff:fff0::
The prefix /29 is equal to netmask ffff:fff8::
The prefix /30 is equal to netmask ffff:fffc::
The prefix /31 is equal to netmask ffff:fffe::
The prefix /32 is equal to netmask ffff:ffff::
The prefix /33 is equal to netmask ffff:ffff:8000::
The prefix /34 is equal to netmask ffff:ffff:c000::
The prefix /35 is equal to netmask ffff:ffff:e000::
The prefix /36 is equal to netmask ffff:ffff:f000::
The prefix /37 is equal to netmask ffff:ffff:f800::
The prefix /38 is equal to netmask ffff:ffff:fc00::
The prefix /39 is equal to netmask ffff:ffff:fe00::
The prefix /40 is equal to netmask ffff:ffff:ff00::
The prefix /41 is equal to netmask ffff:ffff:ff80::
The prefix /42 is equal to netmask ffff:ffff:ffc0::
The prefix /43 is equal to netmask ffff:ffff:ffe0::
The prefix /44 is equal to netmask ffff:ffff:fff0::
The prefix /45 is equal to netmask ffff:ffff:fff8::
The prefix /46 is equal to netmask ffff:ffff:fffc::
The prefix /47 is equal to netmask ffff:ffff:fffe::
The prefix /48 is equal to netmask ffff:ffff:ffff::
The prefix /49 is equal to netmask ffff:ffff:ffff:8000::
The prefix /50 is equal to netmask ffff:ffff:ffff:c000::
The prefix /51 is equal to netmask ffff:ffff:ffff:e000::
The prefix /52 is equal to netmask ffff:ffff:ffff:f000::
The prefix /53 is equal to netmask ffff:ffff:ffff:f800::
The prefix /54 is equal to netmask ffff:ffff:ffff:fc00::
The prefix /55 is equal to netmask ffff:ffff:ffff:fe00::
The prefix /56 is equal to netmask ffff:ffff:ffff:ff00::
The prefix /57 is equal to netmask ffff:ffff:ffff:ff80::
The prefix /58 is equal to netmask ffff:ffff:ffff:ffc0::
The prefix /59 is equal to netmask ffff:ffff:ffff:ffe0::
The prefix /60 is equal to netmask ffff:ffff:ffff:fff0::
The prefix /61 is equal to netmask ffff:ffff:ffff:fff8::
The prefix /62 is equal to netmask ffff:ffff:ffff:fffc::
The prefix /63 is equal to netmask ffff:ffff:ffff:fffe::
The prefix /64 is equal to netmask ffff:ffff:ffff:ffff::
The prefix /65 is equal to netmask ffff:ffff:ffff:ffff:8000::
The prefix /66 is equal to netmask ffff:ffff:ffff:ffff:c000::
The prefix /67 is equal to netmask ffff:ffff:ffff:ffff:e000::
The prefix /68 is equal to netmask ffff:ffff:ffff:ffff:f000::
The prefix /69 is equal to netmask ffff:ffff:ffff:ffff:f800::
The prefix /70 is equal to netmask ffff:ffff:ffff:ffff:fc00::
The prefix /71 is equal to netmask ffff:ffff:ffff:ffff:fe00::
The prefix /72 is equal to netmask ffff:ffff:ffff:ffff:ff00::
The prefix /73 is equal to netmask ffff:ffff:ffff:ffff:ff80::
The prefix /74 is equal to netmask ffff:ffff:ffff:ffff:ffc0::
The prefix /75 is equal to netmask ffff:ffff:ffff:ffff:ffe0::
The prefix /76 is equal to netmask ffff:ffff:ffff:ffff:fff0::
The prefix /77 is equal to netmask ffff:ffff:ffff:ffff:fff8::
The prefix /78 is equal to netmask ffff:ffff:ffff:ffff:fffc::
The prefix /79 is equal to netmask ffff:ffff:ffff:ffff:fffe::
The prefix /80 is equal to netmask ffff:ffff:ffff:ffff:ffff::
The prefix /81 is equal to netmask ffff:ffff:ffff:ffff:ffff:8000::
The prefix /82 is equal to netmask ffff:ffff:ffff:ffff:ffff:c000::
The prefix /83 is equal to netmask ffff:ffff:ffff:ffff:ffff:e000::
The prefix /84 is equal to netmask ffff:ffff:ffff:ffff:ffff:f000::
The prefix /85 is equal to netmask ffff:ffff:ffff:ffff:ffff:f800::
The prefix /86 is equal to netmask ffff:ffff:ffff:ffff:ffff:fc00::
The prefix /87 is equal to netmask ffff:ffff:ffff:ffff:ffff:fe00::
The prefix /88 is equal to netmask ffff:ffff:ffff:ffff:ffff:ff00::
The prefix /89 is equal to netmask ffff:ffff:ffff:ffff:ffff:ff80::
The prefix /90 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffc0::
The prefix /91 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffe0::
The prefix /92 is equal to netmask ffff:ffff:ffff:ffff:ffff:fff0::
The prefix /93 is equal to netmask ffff:ffff:ffff:ffff:ffff:fff8::
The prefix /94 is equal to netmask ffff:ffff:ffff:ffff:ffff:fffc::
The prefix /95 is equal to netmask ffff:ffff:ffff:ffff:ffff:fffe::
The prefix /96 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff::
The prefix /97 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:8000:0
The prefix /98 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:c000:0
The prefix /99 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:e000:0
The prefix /100 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:f000:0
The prefix /101 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:f800:0
The prefix /102 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:fc00:0
The prefix /103 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:fe00:0
The prefix /104 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ff00:0
The prefix /105 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ff80:0
The prefix /106 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffc0:0
The prefix /107 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffe0:0
The prefix /108 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:fff0:0
The prefix /109 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:fff8:0
The prefix /110 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:fffc:0
The prefix /111 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:fffe:0
The prefix /112 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:0
The prefix /113 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:8000
The prefix /114 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:c000
The prefix /115 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:e000
The prefix /116 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:f000
The prefix /117 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:f800
The prefix /118 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:fc00
The prefix /119 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:fe00
The prefix /120 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00
The prefix /121 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff80
The prefix /122 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffc0
The prefix /123 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffe0
The prefix /124 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0
The prefix /125 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff8
The prefix /126 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc
The prefix /127 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe
The last prefix /128 is equal to netmask ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
这意味着,如果用户提供输入(网络掩码)8000::
,则regex将验证它是否为valid
;如果用户提供输入(网络掩码)800a::
,则regex将声明它无效,因为它不在上述网络掩码范围内
这是我得到的当前正则表达式:
1.此输出显示correct
,这是正常的
#This is netmask given by user:
ipv6_netmask_given="c000:0000:0000:0000:0000:0000:0000:0000" # equal to c000:: and it is inside the range
ipv6_netmask_regex='^([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4})$'
if [[ ${ipv6_netmask_given} =~ ${ipv6_netmask_regex} ]]; then
echo "correct"
else
echo "invalid netmask"
fi
# output: correct
1.此输出显示correct
,但显示SHOULD NOT
,因为给定的网络掩码cf01::
不在网络掩码范围内
# This is netmask given by user:
ipv6_netmask_given="cf01:0000:0000:0000:0000:0000:0000:0000"
ipv6_netmask_regex='^([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4})$'
if [[ ${ipv6_netmask_given} =~ ${ipv6_netmask_regex} ]]; then
echo "correct"
else
echo "invalid netmask"
fi
# output: correct
# expected output: invalid netmask
我知道这里的正则表达式太通用了,但我如何确保正则表达式只能接受1到128个IP之间的有效网络掩码?
如果我使用IPv4,我没有问题,因为我可以解决类似的问题,我现在有.例如,这是IPv4正则表达式验证网络掩码,它是工作正常:
'^(128|192|224|240|248|252|254|255)\.0\.0\.0|255\.(0|128|192|224|240|248|252|254|255)\.0\.0|255\.255\.(0|128|192|224|240|248|252|254|255)\.0|255\.255\.255\.(0|128|192|224|240|248|252|254|255)$'
上面的正则表达式接受128.0.0.0
到255.255.255.255
网络掩码之间的32个IP。因此,它不会接受来自不在子网/网络掩码列表中的用户的子网/网络掩码IP,如128.0.0.1
:
但是对于IPv6,如何改进我的正则表达式,使其能够正确验证IPv6子网?
从我上面的问题简单地说:
我如何告诉regex只接受我上面发布的网络掩码范围列表?
谢啦,谢啦
1条答案
按热度按时间aiazj4mn1#
你可以分两步来处理多个需求,这里严格遵守你给出的第一个列表(只有那些匹配的):
第一个月
^(?:(?:[^:]{4}:){1,6}:|(?:[^:]{4}:){7}(?:0|[^:]{4}))$
因此,您的脚本可能要求输入由两个正则表达式验证:
下面是允许更多零的工作方式: