Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C IPv6地址的正则表达式_C_Regex_Networking_Ipv6_Flex Lexer - Fatal编程技术网

C IPv6地址的正则表达式

C IPv6地址的正则表达式,c,regex,networking,ipv6,flex-lexer,C,Regex,Networking,Ipv6,Flex Lexer,我有一个IPv6地址的正则表达式,如下所示 IPV4ADDRESS [ \t]*(([[:digit:]]{1,3}"."){3}([[:digit:]]{1,3}))[ \t]* x4 ([[:xdigit:]]{1,4}) xseq ({x4}(:{x4}){0,7}) xpart ({xseq}|({xseq}::({xseq}?))|::{xseq}) IPV6ADDRESS [ \t]*({x

我有一个IPv6地址的正则表达式,如下所示

IPV4ADDRESS      [ \t]*(([[:digit:]]{1,3}"."){3}([[:digit:]]{1,3}))[ \t]*
x4               ([[:xdigit:]]{1,4})
xseq             ({x4}(:{x4}){0,7})
xpart            ({xseq}|({xseq}::({xseq}?))|::{xseq})
IPV6ADDRESS      [ \t]*({xpart}(":"{IPV4ADDRESS})?)[ \t]*
它正确支持所有格式的IPv6地址,包括

1) non-compressed IPv6 addresses
2) compressed IPv6 addresses
3) IPv6 addresses in legacy formats.(supporting IPv4)
传统格式的IPv6地址的理想示例如下

2001:1234::3210:5.6.7.8

     OR
2001:1234:1234:5432:4578:5678:5.6.7.8

As you can see above there are 10 groups separated by either `":" or ".".`
与正常IPv6地址中的8个组不同。这是因为最后4个由“.”分隔的组应压缩为IPv6地址的最低有效32位。因此,我们需要10个组来满足128位的要求

但是,如果我使用以下地址格式

   2001:1234:4563:3210:5.6.7.8
此处,由“:”分隔的每个组表示16位。由“.”分隔的最后四个组表示8位。位总数为64+32=96位。缺少32位


正则表达式接受它作为有效的IPv6地址格式。我无法确定如何修复正则表达式以丢弃此类值。非常感谢您的帮助。

以下是IPv6地址的语法,如中所述,随后在中确认:

使用此功能,我们可以为IPv6地址构建符合标准的正则表达式

dec_octet      ([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
ipv4address    ({dec_octet}"."){3}{dec_octet}
h16            ([[:xdigit:]]{1,4})
ls32           ({h16}:{h16}|{ipv4address})
ipv6address    (({h16}:){6}{ls32}|::({h16}:){5}{ls32}|({h16})?::({h16}:){4}{ls32}|(({h16}:){0,1}{h16})?::({h16}:){3}{ls32}|(({h16}:){0,2}{h16})?::({h16}:){2}{ls32}|(({h16}:){0,3}{h16})?::{h16}:{ls32}|(({h16}:){0,4}{h16})?::{ls32}|(({h16}:){0,5}{h16})?::{h16}|(({h16}:){0,6}{h16})?::)

免责声明:未测试。

您能解释一下所提供的否定示例有什么问题吗?在上面的示例中已经解释过了。用“:”分隔的每个组表示16位。用“.”分隔的最后四个组表示8位。总位数为64+32=96位。缺少32位。嗯,它还接受诸如::0:999.999.999.999之类的废话。我在里面有代码来检查ipv4部分是否小于255。+1另请参阅我的文章:其中包括IPv6的正则表达式和中指定的所有其他URI组件。
dec_octet      ([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
ipv4address    ({dec_octet}"."){3}{dec_octet}
h16            ([[:xdigit:]]{1,4})
ls32           ({h16}:{h16}|{ipv4address})
ipv6address    (({h16}:){6}{ls32}|::({h16}:){5}{ls32}|({h16})?::({h16}:){4}{ls32}|(({h16}:){0,1}{h16})?::({h16}:){3}{ls32}|(({h16}:){0,2}{h16})?::({h16}:){2}{ls32}|(({h16}:){0,3}{h16})?::{h16}:{ls32}|(({h16}:){0,4}{h16})?::{ls32}|(({h16}:){0,5}{h16})?::{h16}|(({h16}:){0,6}{h16})?::)