Regex for IP Address

Certified Associate Developer

Hi All,

Anybody can please me for regexmatch code IPv6 Ip Address. I an using this regex code 

"^([0-9a-fA-F]{0,4}:){7}[0-9a-fA-F]{0,4}$"

Using above code this scenario:

a) :2551:FFFF::3333:4566:3456:0 is also vaild but I don't want to start IP address with any special character.

Some of the example below that I want to match regex code valid and invaild like:

a) :2551:FFFF::3333:4566:3456:0 (Invalid)

b) 1001:2551:FFFF:255b:1222:3333:4566:3456 (valid)
     1001:2551:F:255b:1222:3333:4566:3456 (valid)

c) 1001:::255b::::3456  (vaild)

d) 1001:::255b:::456: (invalid)

I need a regex match code which passed all the above mention examples.

Thanks  

  Discussion posts and replies are publicly visible

Parents
  • This will be tricky if you're expecing it to accept all possible combinations including the shortform notation where zero-segments can be skipped with the "::" notation.  I assume it's possible but your match pattern will probably end up quite a bit more complicated.

    A quick google result (leading to StackOverflow) suggests this pattern might work (i'm not 100% sure how Appian-compatible this will be, but it seems to work for me at a first glance)

    (?:^|(?<=\s))(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(?=\s|$)
    

    Edit: i tried it out and it seems to work as-desired for all your examples except "c", though i think your example "c" might actually not be valid without editing (i think maybe only one "::" shortcut is valid in the sequence?  not sure.)

Reply
  • This will be tricky if you're expecing it to accept all possible combinations including the shortform notation where zero-segments can be skipped with the "::" notation.  I assume it's possible but your match pattern will probably end up quite a bit more complicated.

    A quick google result (leading to StackOverflow) suggests this pattern might work (i'm not 100% sure how Appian-compatible this will be, but it seems to work for me at a first glance)

    (?:^|(?<=\s))(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(?=\s|$)
    

    Edit: i tried it out and it seems to work as-desired for all your examples except "c", though i think your example "c" might actually not be valid without editing (i think maybe only one "::" shortcut is valid in the sequence?  not sure.)

Children