可能重复:
What are the Java regular expressions for matching IPv4 and IPv6 strings?
是否有人使用java代码解压缩给定的IPv6地址。
我发现写一个正则表达式真的很难。
答案 0 :(得分:3)
其中一个应该可以帮到你:
public static final String IPV6_HEX4DECCOMPRESSED_REGEX = "\\A((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) ::((?:[0-9A-Fa-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}\\z";
public static final String IPV6_6HEX4DEC_REGEX = "\\A((?:[0-9A-Fa-f]{1,4}:){6,6})(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";
public static final String IPV6_HEXCOMPRESSED_REGEX = "\\A((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)\\z";
public static final String IPV6_REGEX = "\\A(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\\z";
另请查看Inet6Address class,这会让您的生活更轻松。
<强>参考:强>
答案 1 :(得分:0)
为什么在使用Inet6Address.getHostAddress
时使用正则表达式?
final String compressed = "1080::8:800:200c:417a";
final String decompressed = "1080:0:0:0:8:800:200c:417a";
try {
final Inet6Address addr = (Inet6Address) InetAddress.getByName(compressed);
assert addr.getHostAddress().equals(decompressed);
} catch (UnknownHostException ex) { }