1

For example, i have a dozen of IPV6 addresses, with 8 parts which are seperated by ":". If the first few characters are null in these parts then i wanna remove them with a simple regex.

Input:

1034:0123:0000:2100:3120:0000:0022:0001

Output:

1034:123::2100:3120::22:1

Is it possible?

8
  • The regex itself can't make anything shorter, regex is just for pattern matching. What language are you using, so we know what functions to use for the actual replacement? Commented Nov 24, 2014 at 17:55
  • 6
    That IPv6 address is invalid; only one set of double colons can be present. Commented Nov 24, 2014 at 17:58
  • Thanks, i just thought it would be the easiest way. Bash, ShellScript. Commented Nov 24, 2014 at 17:58
  • I suspect he means passing it through sed -e 's/regex/replace/g' Commented Nov 24, 2014 at 17:59
  • Doesn't matter, i just wanted to explain somehow what i wanna get. Commented Nov 24, 2014 at 18:00

2 Answers 2

1

Talked about it in the comments, but it works now so here's the answer.

/(^|:)0{1,4}/g 

That regex should work.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use python's socket module to get

>>> socket.getaddrinfo('1034:0123:0000:2100:3120:0000:0022:0001', 0, socket.AF_INET6) [(10, 1, 6, '', ('1034:123:0:2100:3120:0:22:1', 0, 0, 0)), (10, 2, 17, '', ('1034:123:0:2100:3120:0:22:1', 0, 0, 0)), (10, 3, 0, '', ('1034:123:0:2100:3120:0:22:1', 0, 0, 0))] >>> socket.getaddrinfo('1034:0123:0000:2100:3120:0000:0022:0001', 0, socket.AF_INET6)[0][4][0] '1034:123:0:2100:3120:0:22:1' 

As you can see, the resulting IPv6 address has all the excess zeroes stripped out.

1 Comment

This is actually the correct notation according to tools.ietf.org/html/rfc5952#section-4.2.2. A single :0: should not be compressed to ::

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.