1

Possible Duplicate:
What are the Java regular expressions for matching IPv4 and IPv6 strings?

Does anyone have a java code to decompress a given IPv6 address.

I am finding it really difficult to write a regular expression.

Community
  • 1
  • 1
user1601302
  • 21
  • 2
  • 3

2 Answers2

3

One of these should help you:

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";

Also have a look at Inet6Address class which will make your life easier.

Reference:

Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • David, i did some searching on net and i found a few regular expression. Frankly i am not very comfortable writing regex, for that reason i am looking for a java code. – user1601302 Aug 15 '12 at 20:00
  • @user1601302 have you had a look at the Inet6Address Class link at the bottom of my answer? – David Kroukamp Aug 15 '12 at 20:04
  • Yes David, i saw it. I want to know if i use the Inet6Address class, will thr be a DNS lookup. My situation is user is keying an Address, i need to validate and decompress it. – user1601302 Aug 15 '12 at 21:09
0

Why use regex when you can use 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) { }
obataku
  • 29,212
  • 3
  • 44
  • 57