IPV6地址在Java中压缩成形式 [英] IPV6 address into compressed form in Java

查看:484
本文介绍了IPV6地址在Java中压缩成形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Inet6Address.getByName(2001:db8:0:0:0:0:2:1)。toString()压缩IPv6地址的方法,输出为 2001:db8:0:0:0:0:2:1 ,但我需要 2001:db8 :: 2: 1 。 ,基本上压缩输出应基于 RFC 5952标准,即

I have used Inet6Address.getByName("2001:db8:0:0:0:0:2:1").toString() method to compress IPv6 address, and the output is 2001:db8:0:0:0:0:2:1 ,but i need 2001:db8::2:1 . , Basically the compression output should based on RFC 5952 standard , that is

1) 尽可能缩短可能 :例如,2001:db8:0:0:0:0:2:1必须缩短为

2001:db8 :: 2:1.Likewise, 2001:db8 :: 0:1是不可接受的,
因为符号::可能用于生成
较短的表示形式2001:db8 :: 1。

1) Shorten as Much as Possible : For example, 2001:db8:0:0:0:0:2:1 must be shortened to
2001:db8::2:1.Likewise, 2001:db8::0:1 is not acceptable, because the symbol "::" could have been used to produce a shorter representation 2001:db8::1.

2) 处理一个16位0字段 :符号::不能用于缩短一个16位0字段。
例如,表示形式2001:db8:0:1:1:1:1:1是正确的,但是
2001:db8 :: 1:1:1:1:1不正确。

2) Handling One 16-Bit 0 Field : The symbol "::" MUST NOT be used to shorten just one 16-bit 0 field. For example, the representation 2001:db8:0:1:1:1:1:1 is correct, but 2001:db8::1:1:1:1:1 is not correct.

3) 放置::的选择 =当在::的位置中有另一种选择时,
最长连续16位0字段的运行必须缩短(即
,2001年缩短了三个连续零场的序列:
0:0:1:0:0:0:1)。当连续的16位0字段
的长度相等时(即2001:db8:0:0:1:0:0:1),必须缩短第一个零
位的序列。例如,2001:db8 :: 1:0:0:1是正确的
表示。

3) Choice in Placement of "::" : = When there is an alternative choice in the placement of a "::", the longest run of consecutive 16-bit 0 fields MUST be shortened (i.e., the sequence with three consecutive zero fields is shortened in 2001: 0:0:1:0:0:0:1). When the length of the consecutive 16-bit 0 fields are equal (i.e., 2001:db8:0:0:1:0:0:1), the first sequence of zero bits MUST be shortened. For example, 2001:db8::1:0:0:1 is correct representation.

我还检查了 堆栈溢出中的另一个帖子 ,但没有指定条件(放置的示例选择::)。

I have also checked another post in Stack overflow, but there was no condition specified (example choice in placement of ::).

是否有任何java库来处理这个问题?有人可以帮帮我吗?

Is there any java library to handle this? Could anyone please help me?

提前致谢。

推荐答案

怎么样?

String resultString = subjectString.replaceAll("((?::0\\b){2,}):?(?!\\S*\\b\\1:0\\b)(\\S*)", "::$2");

没有Java双反斜杠的解释地狱:

(       # Match and capture in backreference 1:
 (?:    #  Match this group:
  :0    #  :0
  \b    #  word boundary
 ){2,}  # twice or more
)       # End of capturing group 1
:?      # Match a : if present (not at the end of the address)
(?!     # Now assert that we can't match the following here:
 \S*    #  Any non-space character sequence
 \b     #  word boundary
 \1     #  the previous match
 :0     #  followed by another :0
 \b     #  word boundary
)       # End of lookahead. This ensures that there is not a longer
        # sequence of ":0"s in this address.
(\S*)   # Capture the rest of the address in backreference 2.
        # This is necessary to jump over any sequences of ":0"s
        # that are of the same length as the first one.

输入:

2001:db8:0:0:0:0:2:1
2001:db8:0:1:1:1:1:1
2001:0:0:1:0:0:0:1
2001:db8:0:0:1:0:0:1
2001:db8:0:0:1:0:0:0

输出:

2001:db8::2:1
2001:db8:0:1:1:1:1:1
2001:0:0:1::1
2001:db8::1:0:0:1
2001:db8:0:0:1::

(我希望最后一个例子是正确的 - 或者如果地址以 0 结尾?还有另一条规则吗?)

(I hope the last example is correct - or is there another rule if the address ends in 0?)

这篇关于IPV6地址在Java中压缩成形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆