<p>I'm having trouble writing a regular expression that matches valid IPv6 addresses, including those in their compressed form (with <code>::</code> or leading zeros omitted from each byte pair).  </p>

<p>Can someone suggest a regular expression that would fulfill the requirement?</p>

<p>I'm considering expanding each byte pair and matching the result with a simpler regex.</p>

[{'answer_id': 53499, 'author': 'Factor Mystic', 'author_id': 1569, 'author_profile': 'https://Stackoverflow.com/users/1569', 'pm_score': 5, 'selected': False, 'text': '<p>From "<a href="http://vernon.mauery.com/content/projects/linux/ipv6_regex" rel="noreferrer">IPv6 regex</a>": </p>\n\n<pre><code>(\\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6}\\Z)|\n(\\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}\\Z)|\n(\\A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}\\Z)|\n(\\A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}\\Z)|\n(\\A([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}\\Z)|\n(\\A([0-9a-f]{1,4}:){1,6}(:[0-9a-f]{1,4}){1,1}\\Z)|\n(\\A(([0-9a-f]{1,4}:){1,7}|:):\\Z)|\n(\\A:(:[0-9a-f]{1,4}){1,7}\\Z)|\n(\\A((([0-9a-f]{1,4}:){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)|\n(\\A(([0-9a-f]{1,4}:){5}[0-9a-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)|\n(\\A([0-9a-f]{1,4}:){5}:[0-9a-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)|\n(\\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){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)|\n(\\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,3}:(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\Z)|\n(\\A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,2}:(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\Z)|\n(\\A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,1}:(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\Z)|\n(\\A(([0-9a-f]{1,4}:){1,5}|:):(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\Z)|\n(\\A:(:[0-9a-f]{1,4}){1,5}:(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\Z)\n</code></pre>\n'}, {'answer_id': 53578, 'author': 'Jon Cram', 'author_id': 5343, 'author_profile': 'https://Stackoverflow.com/users/5343', 'pm_score': 4, 'selected': False, 'text': '<p>I\'d have to strongly second the answer from <a href="/users/338/frank-krueger">Frank Krueger</a>.</p>\n\n<p>Whilst you say you need a regular expression to match an IPv6 address, I\'m assuming what you really need is to be able to check if a given string is a valid IPv6 address. There is a subtle but important distinction here.</p>\n\n<p>There is more than one way to check if a given string is a valid IPv6 address and regular expression matching is only one solution.</p>\n\n<p>Use an existing library if you can. The library will have fewer bugs and its use will result in less code for you to maintain.</p>\n\n<p>The regular expression suggested by <a href="/users/1569/factor-mystic">Factor Mystic</a> is long and complex. It most likely works, but you should also consider how you\'d cope if it unexpectedly fails. The point I\'m trying to make here is that if you can\'t form a required regular expression yourself you won\'t be able to easily debug it.</p>\n\n<p>If you have no suitable library it may be better to write your own IPv6 validation routine that doesn\'t depend on regular expressions. If you write it you understand it and if you understand it you can add comments to explain it so that others can also understand and subsequently maintain it.</p>\n\n<p>Act with caution when using a regular expression whose functionality you can\'t explain to someone else.</p>\n'}, {'answer_id': 60946, 'author': 'Brad Gilbert', 'author_id': 1337, 'author_profile': 'https://Stackoverflow.com/users/1337', 'pm_score': 3, 'selected': False, 'text': '<p>If you use Perl try <a href="https://metacpan.org/module/Net::IPv6Addr" rel="nofollow noreferrer">Net::IPv6Addr</a></p>\n\n<pre><code>use Net::IPv6Addr;\n\nif( defined Net::IPv6Addr::is_ipv6($ip_address) ){\n  print "Looks like an ipv6 address\\n";\n}\n</code></pre>\n\n<p><a href="https://metacpan.org/module/NetAddr::IP" rel="nofollow noreferrer">NetAddr::IP</a></p>\n\n<pre><code>use NetAddr::IP;\n\nmy $obj = NetAddr::IP-&gt;new6($ip_address);\n</code></pre>\n\n<p><a href="https://metacpan.org/module/Data::Validate::IP" rel="nofollow noreferrer">Validate::IP</a></p>\n\n<pre><code>use Validate::IP qw\'is_ipv6\';\n\nif( is_ipv6($ip_address) ){\n  print "Looks like an ipv6 address\\n";\n}\n</code></pre>\n'}, {'answer_id': 81899, 'author': 'Joe Hildebrand', 'author_id': 8388, 'author_profile': 'https://Stackoverflow.com/users/8388', 'pm_score': 5, 'selected': False, 'text': '<p>It sounds like you may be using Python.  If so, you can use something like this:</p>\n\n<pre><code>import socket\n\ndef check_ipv6(n):\n    try:\n        socket.inet_pton(socket.AF_INET6, n)\n        return True\n    except socket.error:\n        return False\n\nprint check_ipv6(\'::1\') # True\nprint check_ipv6(\'foo\') # False\nprint check_ipv6(5)     # TypeError exception\nprint check_ipv6(None)  # TypeError exception\n</code></pre>\n\n<p>I don\'t think you have to have IPv6 compiled in to Python to get <a href="http://docs.python.org/2/library/socket.html#socket.inet_pton" rel="noreferrer"><code>inet_pton</code></a>, which can also parse IPv4 addresses if you pass in <code>socket.AF_INET</code> as the first parameter.  Note: this may not work on non-Unix systems.</p>\n'}, {'answer_id': 1572953, 'author': 'Aeron', 'author_id': 190680, 'author_profile': 'https://Stackoverflow.com/users/190680', 'pm_score': -1, 'selected': False, 'text': '<p>The regex allows the use of leading zeros in the IPv4 parts.</p>\n\n<p>Some Unix and Mac distros convert those segments into octals.</p>\n\n<p>I suggest using <code>25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d</code> as an IPv4 segment.</p>\n'}, {'answer_id': 1934546, 'author': 'Michael', 'author_id': 215384, 'author_profile': 'https://Stackoverflow.com/users/215384', 'pm_score': 6, 'selected': False, 'text': "<p>The following will validate IPv4, IPv6 (full and compressed), and IPv6v4 (full and compressed) addresses:</p>\n\n<pre><code>'/^(?&gt;(?&gt;([a-f0-9]{1,4})(?&gt;:(?1)){7}|(?!(?:.*[a-f0-9](?&gt;:|$)){8,})((?1)(?&gt;:(?1)){0,6})?::(?2)?)|(?&gt;(?&gt;(?1)(?&gt;:(?1)){5}:|(?!(?:.*[a-f0-9]:){6,})(?3)?::(?&gt;((?1)(?&gt;:(?1)){0,4}):)?)?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?&gt;\\.(?4)){3}))$/iD'\n</code></pre>\n"}, {'answer_id': 3837430, 'author': 'user463639', 'author_id': 463639, 'author_profile': 'https://Stackoverflow.com/users/463639', 'pm_score': 1, 'selected': False, 'text': '<p>In Java, you can use the library class <code>sun.net.util.IPAddressUtil</code>:</p>\n\n<pre><code>IPAddressUtil.isIPv6LiteralAddress(iPaddress);\n</code></pre>\n'}, {'answer_id': 5567938, 'author': 'janCoffee', 'author_id': 513481, 'author_profile': 'https://Stackoverflow.com/users/513481', 'pm_score': 3, 'selected': False, 'text': '<p>This regular expression will match valid IPv6 and IPv4 addresses in accordance with GNU C++ implementation of regex with REGULAR EXTENDED mode used:</p>\n\n<pre><code>"^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}))|:)))(%.+)?\\s*$"\n</code></pre>\n'}, {'answer_id': 14365005, 'author': 'Ahamx', 'author_id': 1984714, 'author_profile': 'https://Stackoverflow.com/users/1984714', 'pm_score': 1, 'selected': False, 'text': '<p>Using Ruby? Try this:</p>\n\n<pre><code>/^(((?=.*(::))(?!.*\\3.+\\3))\\3?|[\\dA-F]{1,4}:)([\\dA-F]{1,4}(\\3|:\\b)|\\2){5}(([\\dA-F]{1,4}(\\3|:\\b|$)|\\2){2}|(((2[0-4]|1\\d|[1-9])?\\d|25[0-5])\\.?\\b){4})\\z/i\n</code></pre>\n'}, {'answer_id': 14960927, 'author': 'Remi Morin', 'author_id': 2087666, 'author_profile': 'https://Stackoverflow.com/users/2087666', 'pm_score': 3, 'selected': False, 'text': '<p>I\'m not an Ipv6 expert but I think you can get a pretty good result more easily with this one:</p>\n\n<pre><code>^([0-9A-Fa-f]{0,4}:){2,7}([0-9A-Fa-f]{1,4}$|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4})$\n</code></pre>\n\n<p>to answer "is a valid ipv6" it look like ok to me. To break it down in parts... forget it. I\'ve omitted the unspecified one (::) since  there is no use to have "unpecified adress" in my database. </p>\n\n<p>the beginning:\n<code>^([0-9A-Fa-f]{0,4}:){2,7}</code> &lt;-- match the compressible part, we can translate this as: between 2 and 7 colon who may have heaxadecimal number between them.</p>\n\n<p>followed by:\n<code>[0-9A-Fa-f]{1,4}$</code> &lt;-- an hexadecimal number (leading 0 omitted)\nOR\n<code>((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}</code> &lt;-- an Ipv4 adress</p>\n'}, {'answer_id': 15988275, 'author': 'JinnKo', 'author_id': 506757, 'author_profile': 'https://Stackoverflow.com/users/506757', 'pm_score': 3, 'selected': False, 'text': "<p>A simple regex that will match, but I wouldn't recommend for validation of any sort is this:</p>\n\n<pre><code>([A-Fa-f0-9]{1,4}::?){1,7}[A-Fa-f0-9]{1,4}\n</code></pre>\n\n<p>Note this matches compression anywhere in the address, though it won't match the loopback address ::1.  I find this a reasonable compromise in order to keep the regex simple.</p>\n\n<p>I successfully use this in iTerm2 <em>smart selection rules</em> to quad-click IPv6 addresses.</p>\n"}, {'answer_id': 17836822, 'author': 'Harry', 'author_id': 126537, 'author_profile': 'https://Stackoverflow.com/users/126537', 'pm_score': -1, 'selected': False, 'text': '<p>If you want only normal IP-s (no slashes), here:</p>\n\n<pre><code>^(?:[0-9a-f]{1,4}(?:::)?){0,7}::[0-9a-f]+$\n</code></pre>\n\n<p>I use it for my syntax highlighter in hosts file editor application. Works as charm.</p>\n'}, {'answer_id': 17871737, 'author': 'David M. Syzdek', 'author_id': 903194, 'author_profile': 'https://Stackoverflow.com/users/903194', 'pm_score': 8, 'selected': False, 'text': '<p>I was unable to get @Factor Mystic\'s answer to work with POSIX regular expressions, so I wrote one that works with POSIX regular expressions and PERL regular expressions.</p>\n<p>It should match:</p>\n<ul>\n<li>IPv6 addresses</li>\n<li>zero compressed IPv6 addresses (<a href="https://www.rfc-editor.org/rfc/rfc5952#section-2.2" rel="noreferrer">section 2.2 of rfc5952</a>)</li>\n<li>link-local IPv6 addresses with zone index (<a href="https://www.rfc-editor.org/rfc/rfc4007#section-11" rel="noreferrer">section 11 of rfc4007</a>)</li>\n<li>IPv4-Embedded IPv6 Address (<a href="https://www.rfc-editor.org/rfc/rfc6052#section-2" rel="noreferrer">section 2 of rfc6052</a>)</li>\n<li>IPv4-mapped IPv6 addresses (<a href="https://www.rfc-editor.org/rfc/rfc2765#section-2.1" rel="noreferrer">section 2.1 of rfc2765</a>)</li>\n<li>IPv4-translated addresses  (<a href="https://www.rfc-editor.org/rfc/rfc2765#section-2.1" rel="noreferrer">section 2.1 of rfc2765</a>)</li>\n</ul>\n<p>IPv6 Regular Expression:</p>\n<pre><code>(([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]))\n</code></pre>\n<p>For ease of reading, the following is the above regular expression split at major OR points into separate lines:</p>\n<pre><code># IPv6 RegEx\n(\n([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|          # 1:2:3:4:5:6:7:8\n([0-9a-fA-F]{1,4}:){1,7}:|                         # 1::                              1:2:3:4:5:6:7::\n([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|         # 1::8             1:2:3:4:5:6::8  1:2:3:4:5:6::8\n([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|  # 1::7:8           1:2:3:4:5::7:8  1:2:3:4:5::8\n([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|  # 1::6:7:8         1:2:3:4::6:7:8  1:2:3:4::8\n([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|  # 1::5:6:7:8       1:2:3::5:6:7:8  1:2:3::8\n([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|  # 1::4:5:6:7:8     1:2::4:5:6:7:8  1:2::8\n[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|       # 1::3:4:5:6:7:8   1::3:4:5:6:7:8  1::8  \n:((:[0-9a-fA-F]{1,4}){1,7}|:)|                     # ::2:3:4:5:6:7:8  ::2:3:4:5:6:7:8 ::8       ::     \nfe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|     # fe80::7:8%eth0   fe80::7:8%1     (link-local IPv6 addresses with zone index)\n::(ffff(:0{1,4}){0,1}:){0,1}\n((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}\n(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|          # ::255.255.255.255   ::ffff:255.255.255.255  ::ffff:0:255.255.255.255  (IPv4-mapped IPv6 addresses and IPv4-translated addresses)\n([0-9a-fA-F]{1,4}:){1,4}:\n((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}\n(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])           # 2001:db8:3:4::192.0.2.33  64:ff9b::192.0.2.33 (IPv4-Embedded IPv6 Address)\n)\n\n# IPv4 RegEx\n((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])\n</code></pre>\n<p>To make the above easier to understand, the following &quot;pseudo&quot; code replicates the above:</p>\n<pre><code>IPV4SEG  = (25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\nIPV4ADDR = (IPV4SEG\\.){3,3}IPV4SEG\nIPV6SEG  = [0-9a-fA-F]{1,4}\nIPV6ADDR = (\n           (IPV6SEG:){7,7}IPV6SEG|                # 1:2:3:4:5:6:7:8\n           (IPV6SEG:){1,7}:|                      # 1::                                 1:2:3:4:5:6:7::\n           (IPV6SEG:){1,6}:IPV6SEG|               # 1::8               1:2:3:4:5:6::8   1:2:3:4:5:6::8\n           (IPV6SEG:){1,5}(:IPV6SEG){1,2}|        # 1::7:8             1:2:3:4:5::7:8   1:2:3:4:5::8\n           (IPV6SEG:){1,4}(:IPV6SEG){1,3}|        # 1::6:7:8           1:2:3:4::6:7:8   1:2:3:4::8\n           (IPV6SEG:){1,3}(:IPV6SEG){1,4}|        # 1::5:6:7:8         1:2:3::5:6:7:8   1:2:3::8\n           (IPV6SEG:){1,2}(:IPV6SEG){1,5}|        # 1::4:5:6:7:8       1:2::4:5:6:7:8   1:2::8\n           IPV6SEG:((:IPV6SEG){1,6})|             # 1::3:4:5:6:7:8     1::3:4:5:6:7:8   1::8\n           :((:IPV6SEG){1,7}|:)|                  # ::2:3:4:5:6:7:8    ::2:3:4:5:6:7:8  ::8       ::       \n           fe80:(:IPV6SEG){0,4}%[0-9a-zA-Z]{1,}|  # fe80::7:8%eth0     fe80::7:8%1  (link-local IPv6 addresses with zone index)\n           ::(ffff(:0{1,4}){0,1}:){0,1}IPV4ADDR|  # ::255.255.255.255  ::ffff:255.255.255.255  ::ffff:0:255.255.255.255 (IPv4-mapped IPv6 addresses and IPv4-translated addresses)\n           (IPV6SEG:){1,4}:IPV4ADDR               # 2001:db8:3:4::192.0.2.33  64:ff9b::192.0.2.33 (IPv4-Embedded IPv6 Address)\n           )\n</code></pre>\n<p>I posted a script on GitHub which tests the regular expression: <a href="https://gist.github.com/syzdek/6086792" rel="noreferrer">https://gist.github.com/syzdek/6086792</a></p>\n'}, {'answer_id': 17895611, 'author': 'user2623580', 'author_id': 2623580, 'author_profile': 'https://Stackoverflow.com/users/2623580', 'pm_score': 3, 'selected': False, 'text': '<p>Beware! <strong>In Java, the use of InetAddress and related classes (Inet4Address, Inet6Address, URL) may involve network trafic!</strong> E.g. DNS resolving (URL.equals, InetAddress from string!). This call may take long and is blocking!</p>\n\n<p>For IPv6 I have something like this. This of course does not handle the very subtle details of IPv6 like that zone indices are allowed only on some classes of IPv6 addresses. And this regex is not written for group capturing, it is only a "matches" kind of regexp.</p>\n\n<p><code>S</code> - IPv6 segment = <code>[0-9a-f]{1,4}</code></p>\n\n<p><code>I</code> - IPv4 = <code>(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2})\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2})</code></p>\n\n<p>Schematic (first part matches IPv6 addresses with IPv4 suffix, second part matches IPv6 addresses, last patrt the zone index):</p>\n\n<pre><code>(\n(\n::(S:){0,5}|\nS::(S:){0,4}|\n(S:){2}:(S:){0,3}|\n(S:){3}:(S:){0,2}|\n(S:){4}:(S:)?|\n(S:){5}:|\n(S:){6}\n)\nI\n\n|\n\n:(:|(:S){1,7})|\nS:(:|(:S){1,6})|\n(S:){2}(:|(:S){1,5})|\n(S:){3}(:|(:S){1,4})|\n(S:){4}(:|(:S){1,3})|\n(S:){5}(:|(:S){1,2})|\n(S:){6}(:|(:S))|\n(S:){7}:|\n(S:){7}S\n)\n\n(?:%[0-9a-z]+)?\n</code></pre>\n\n<p>And here the might regex (case insensitive, surround with what ever needed like beginning/end of line, etc.):</p>\n\n<pre><code>(?:\n(?:\n::(?:[0-9a-f]{1,4}:){0,5}|\n[0-9a-f]{1,4}::(?:[0-9a-f]{1,4}:){0,4}|\n(?:[0-9a-f]{1,4}:){2}:(?:[0-9a-f]{1,4}:){0,3}|\n(?:[0-9a-f]{1,4}:){3}:(?:[0-9a-f]{1,4}:){0,2}|\n(?:[0-9a-f]{1,4}:){4}:(?:[0-9a-f]{1,4}:)?|\n(?:[0-9a-f]{1,4}:){5}:|\n(?:[0-9a-f]{1,4}:){6}\n)\n(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2})\\.){3}\n(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2})|\n\n:(?::|(?::[0-9a-f]{1,4}){1,7})|\n[0-9a-f]{1,4}:(?::|(?::[0-9a-f]{1,4}){1,6})|\n(?:[0-9a-f]{1,4}:){2}(?::|(?::[0-9a-f]{1,4}){1,5})|\n(?:[0-9a-f]{1,4}:){3}(?::|(?::[0-9a-f]{1,4}){1,4})|\n(?:[0-9a-f]{1,4}:){4}(?::|(?::[0-9a-f]{1,4}){1,3})|\n(?:[0-9a-f]{1,4}:){5}(?::|(?::[0-9a-f]{1,4}){1,2})|\n(?:[0-9a-f]{1,4}:){6}(?::|(?::[0-9a-f]{1,4}))|\n(?:[0-9a-f]{1,4}:){7}:|\n(?:[0-9a-f]{1,4}:){7}[0-9a-f]{1,4}\n)\n\n(?:%[0-9a-z]+)?\n</code></pre>\n'}, {'answer_id': 20059655, 'author': 'Wireblue', 'author_id': 367806, 'author_profile': 'https://Stackoverflow.com/users/367806', 'pm_score': 0, 'selected': False, 'text': "<p>For PHP 5.2+ users <code>filter_var</code> works great.</p>\n\n<p>I know this doesn't answer the original question (specifically a regex solution), but I post this in the hope it may help someone else in the future.</p>\n\n<pre><code>$is_ip4address = (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== FALSE);\n$is_ip6address = (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== FALSE);\n</code></pre>\n"}, {'answer_id': 20423004, 'author': 'Chris', 'author_id': 3074256, 'author_profile': 'https://Stackoverflow.com/users/3074256', 'pm_score': -1, 'selected': False, 'text': '<p>This will work for IPv4 and IPv6:</p>\n\n<pre><code>^(([0-9a-f]{0,4}:){1,7}[0-9a-f]{1,4}|([0-9]{1,3}\\.){3}[0-9]{1,3})$\n</code></pre>\n'}, {'answer_id': 21962114, 'author': 'Phil L.', 'author_id': 3024786, 'author_profile': 'https://Stackoverflow.com/users/3024786', 'pm_score': -1, 'selected': False, 'text': '<p>You can use <a href="https://github.com/philpraxis/ipextract" rel="nofollow">the ipextract shell tools</a> I made for this purpose. They are based on regexp and grep.</p>\n\n<p>Usage:</p>\n\n<pre><code>$ ifconfig | ipextract6\nfe80::1%lo0\n::1\nfe80::7ed1:c3ff:feec:dee1%en0\n</code></pre>\n'}, {'answer_id': 22329731, 'author': 'Steve Buzonas', 'author_id': 816584, 'author_profile': 'https://Stackoverflow.com/users/816584', 'pm_score': 2, 'selected': False, 'text': '<p>Looking at the patterns included in the other answers there are a number of good patterns that can be improved by referencing groups and utilizing lookaheads.  Here is an example of a pattern that is self referencing that I would utilize in PHP if I had to:</p>\n\n<pre><code>^(?&lt;hgroup&gt;(?&lt;hex&gt;[[:xdigit:]]{0,4}) # grab a sequence of up to 4 hex digits\n                                     # and name this pattern for usage later\n     (?&lt;!:::):{1,2})                 # match 1 or 2 \':\' characters\n                                     # as long as we can\'t match 3\n (?&amp;hgroup){1,6} # match our hex group 1 to 6 more times\n (?:(?:\n    # match an ipv4 address or\n    (?&lt;dgroup&gt;2[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3}(?&amp;dgroup)\n    # match our hex group one last time\n    |(?&amp;hex))$\n</code></pre>\n\n<p><strong>Note:</strong> PHP has a built in filter for this which would be a better solution than this \npattern.</p>\n\n<p><a href="http://regex101.com/r/qZ5wF3" rel="nofollow">Regex101 Analysis</a></p>\n'}, {'answer_id': 26105334, 'author': 'user1977022', 'author_id': 1977022, 'author_profile': 'https://Stackoverflow.com/users/1977022', 'pm_score': 0, 'selected': False, 'text': "<p>Here's what I came up with, using a bit of lookahead and named groups.  This is of course just IPv6, but it shouldn't interfere with additional patterns if you want to add IPv4:</p>\n\n<pre><code>(?=([0-9a-f]+(:[0-9a-f])*)?(?P&lt;wild&gt;::)(?!([0-9a-f]+:)*:))(::)?([0-9a-f]{1,4}:{1,2}){0,6}(?(wild)[0-9a-f]{0,4}|[0-9a-f]{1,4}:[0-9a-f]{1,4})\n</code></pre>\n"}, {'answer_id': 28289439, 'author': 'Nuh Metin Güler', 'author_id': 4506317, 'author_profile': 'https://Stackoverflow.com/users/4506317', 'pm_score': 1, 'selected': False, 'text': '<p>It is difficult to find a regular expression which works for all IPv6 cases. They are usually hard to maintain, not easily readable and may cause performance problems. Hence, I want to share an alternative solution which I have developed: <a href="https://stackoverflow.com/questions/21631669/regular-expression-regex-for-ipv6-separate-from-ipv4/28289229#28289229">Regular Expression (RegEx) for IPv6 Separate from IPv4</a> </p>\n\n<p>Now you may ask that "This method only finds IPv6, how can I find IPv6 in a text or file?" Here are methods for this issue too.</p>\n\n<p><strong>Note</strong>: If you do not want to use IPAddress class in .NET, you can also replace it with <a href="https://stackoverflow.com/questions/21631669/regular-expression-regex-for-ipv6-separate-from-ipv4/28289229#28289229">my method</a>. It also covers mapped IPv4 and special cases too, while IPAddress does not cover. </p>\n\n<pre><code>class IPv6\n{\n    public List&lt;string&gt; FindIPv6InFile(string filePath)\n    {\n        Char ch;\n        StringBuilder sbIPv6 = new StringBuilder();\n        List&lt;string&gt; listIPv6 = new List&lt;string&gt;();\n        StreamReader reader = new StreamReader(filePath);\n        do\n        {\n            bool hasColon = false;\n            int length = 0;\n\n            do\n            {\n                ch = (char)reader.Read();\n\n                if (IsEscapeChar(ch))\n                    break;\n\n                //Check the first 5 chars, if it has colon, then continue appending to stringbuilder\n                if (!hasColon &amp;&amp; length &lt; 5)\n                {\n                    if (ch == \':\')\n                    {\n                        hasColon = true;\n                    }\n                    sbIPv6.Append(ch.ToString());\n                }\n                else if (hasColon) //if no colon in first 5 chars, then dont append to stringbuilder\n                {\n                    sbIPv6.Append(ch.ToString());\n                }\n\n                length++;\n\n            } while (!reader.EndOfStream);\n\n            if (hasColon &amp;&amp; !listIPv6.Contains(sbIPv6.ToString()) &amp;&amp; IsIPv6(sbIPv6.ToString()))\n            {\n                listIPv6.Add(sbIPv6.ToString());\n            }\n\n            sbIPv6.Clear();\n\n        } while (!reader.EndOfStream);\n        reader.Close();\n        reader.Dispose();\n\n        return listIPv6;\n    }\n\n    public List&lt;string&gt; FindIPv6InText(string text)\n    {\n        StringBuilder sbIPv6 = new StringBuilder();\n        List&lt;string&gt; listIPv6 = new List&lt;string&gt;();\n\n        for (int i = 0; i &lt; text.Length; i++)\n        {\n            bool hasColon = false;\n            int length = 0;\n\n            do\n            {\n                if (IsEscapeChar(text[length + i]))\n                    break;\n\n                //Check the first 5 chars, if it has colon, then continue appending to stringbuilder\n                if (!hasColon &amp;&amp; length &lt; 5)\n                {\n                    if (text[length + i] == \':\')\n                    {\n                        hasColon = true;\n                    }\n                    sbIPv6.Append(text[length + i].ToString());\n                }\n                else if (hasColon) //if no colon in first 5 chars, then dont append to stringbuilder\n                {\n                    sbIPv6.Append(text[length + i].ToString());\n                }\n\n                length++;\n\n            } while (i + length != text.Length);\n\n            if (hasColon &amp;&amp; !listIPv6.Contains(sbIPv6.ToString()) &amp;&amp; IsIPv6(sbIPv6.ToString()))\n            {\n                listIPv6.Add(sbIPv6.ToString());\n            }\n\n            i += length;\n            sbIPv6.Clear();\n        }\n\n        return listIPv6;\n    }\n\n    bool IsEscapeChar(char ch)\n    {\n        if (ch != \' \' &amp;&amp; ch != \'\\r\' &amp;&amp; ch != \'\\n\' &amp;&amp; ch!=\'\\t\')\n        {\n            return false;\n        }\n\n        return true;\n    }\n\n    bool IsIPv6(string maybeIPv6)\n    {\n        IPAddress ip;\n        if (IPAddress.TryParse(maybeIPv6, out ip))\n        {\n            return ip.AddressFamily == AddressFamily.InterNetworkV6;\n        }\n        else\n        {\n            return false;\n        }\n    }\n\n}\n</code></pre>\n'}, {'answer_id': 28711926, 'author': 'user4604205', 'author_id': 4604205, 'author_profile': 'https://Stackoverflow.com/users/4604205', 'pm_score': 1, 'selected': False, 'text': '<p><code>InetAddressUtils</code> has all the patterns defined. I ended-up using their pattern directly, and am pasting it here for reference:</p>\n\n<pre><code>private static final String IPV4_BASIC_PATTERN_STRING =\n        "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\\\.){3}" + // initial 3 fields, 0-255 followed by .\n         "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"; // final field, 0-255\n\nprivate static final Pattern IPV4_PATTERN =\n    Pattern.compile("^" + IPV4_BASIC_PATTERN_STRING + "$");\n\nprivate static final Pattern IPV4_MAPPED_IPV6_PATTERN = // TODO does not allow for redundant leading zeros\n        Pattern.compile("^::[fF]{4}:" + IPV4_BASIC_PATTERN_STRING + "$");\n\nprivate static final Pattern IPV6_STD_PATTERN =\n    Pattern.compile(\n            "^[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}$");\n\nprivate static final Pattern IPV6_HEX_COMPRESSED_PATTERN =\n    Pattern.compile(\n            "^(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)" + // 0-6 hex fields\n             "::" +\n             "(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)$"); // 0-6 hex fields \n</code></pre>\n'}, {'answer_id': 32970482, 'author': 'OliverKK', 'author_id': 2717428, 'author_profile': 'https://Stackoverflow.com/users/2717428', 'pm_score': 2, 'selected': False, 'text': '<p>In <strong>Scala</strong> use the well known Apache Commons validators.</p>\n\n<p><a href="http://mvnrepository.com/artifact/commons-validator/commons-validator/1.4.1" rel="nofollow">http://mvnrepository.com/artifact/commons-validator/commons-validator/1.4.1</a> </p>\n\n<pre><code>libraryDependencies += "commons-validator" % "commons-validator" % "1.4.1"\n\n\nimport org.apache.commons.validator.routines._\n\n/**\n * Validates if the passed ip is a valid IPv4 or IPv6 address.\n *\n * @param ip The IP address to validate.\n * @return True if the passed IP address is valid, false otherwise.\n */  \n def ip(ip: String) = InetAddressValidator.getInstance().isValid(ip)\n</code></pre>\n\n<p>Following the test\'s of the method <code>ip(ip: String)</code>:</p>\n\n<pre><code>"The `ip` validator" should {\n  "return false if the IPv4 is invalid" in {\n    ip("123") must beFalse\n    ip("255.255.255.256") must beFalse\n    ip("127.1") must beFalse\n    ip("30.168.1.255.1") must beFalse\n    ip("-1.2.3.4") must beFalse\n  }\n\n  "return true if the IPv4 is valid" in {\n    ip("255.255.255.255") must beTrue\n    ip("127.0.0.1") must beTrue\n    ip("0.0.0.0") must beTrue\n  }\n\n  //IPv6\n  //@see: http://www.ronnutter.com/ipv6-cheatsheet-on-identifying-valid-ipv6-addresses/\n  "return false if the IPv6 is invalid" in {\n    ip("1200::AB00:1234::2552:7777:1313") must beFalse\n  }\n\n  "return true if the IPv6 is valid" in {\n    ip("1200:0000:AB00:1234:0000:2552:7777:1313") must beTrue\n    ip("21DA:D3:0:2F3B:2AA:FF:FE28:9C5A") must beTrue\n  }\n}\n</code></pre>\n'}, {'answer_id': 37355379, 'author': 'Rohit Malgaonkar', 'author_id': 6336249, 'author_profile': 'https://Stackoverflow.com/users/6336249', 'pm_score': 4, 'selected': False, 'text': '<p>This catches the loopback(::1) as well and ipv6 addresses.\n changed {} to + and put : inside the first square bracket.</p>\n\n<pre><code>([a-f0-9:]+:+)+[a-f0-9]+\n</code></pre>\n\n<p>tested on with ifconfig -a output\n<a href="http://regexr.com/" rel="noreferrer">http://regexr.com/</a></p>\n\n<p>Unix or Mac OSx terminal o option returns only the matching output(ipv6) including ::1</p>\n\n<pre><code>ifconfig -a | egrep -o \'([a-f0-9:]+:+)+[a-f0-9]+\'\n</code></pre>\n\n<p>Get All IP addresses (IPv4 OR IPv6) and print match on unix OSx term</p>\n\n<pre><code>ifconfig -a | egrep -o \'([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) | (([a-f0-9:]+:+)+[a-f0-9]+)\'\n</code></pre>\n'}, {'answer_id': 37903336, 'author': 'Carlos Velazquez', 'author_id': 6484333, 'author_profile': 'https://Stackoverflow.com/users/6484333', 'pm_score': -1, 'selected': False, 'text': '<p>Try this small one-liner. It should only match valid uncompressed/compressed IPv6 addresses (no IPv4 hybrids)</p>\n\n<pre><code>/(?!.*::.*::)(?!.*:::.*)(?!:[a-f0-9])((([a-f0-9]{1,4})?[:](?!:)){7}|(?=(.*:[:a-f0-9]{1,4}::|^([:a-f0-9]{1,4})?::))(([a-f0-9]{1,4})?[:]{1,2}){1,6})[a-f0-9]{1,4}/\n</code></pre>\n'}, {'answer_id': 39237544, 'author': 'Bill Lipa', 'author_id': 1825318, 'author_profile': 'https://Stackoverflow.com/users/1825318', 'pm_score': 2, 'selected': False, 'text': '<p>Depending on your needs, an approximation like:</p>\n\n<pre><code>[0-9a-f:]+\n</code></pre>\n\n<p>may be enough (as with simple log file grepping, for example.)</p>\n'}, {'answer_id': 39841307, 'author': 'Mike Wilmes', 'author_id': 5140523, 'author_profile': 'https://Stackoverflow.com/users/5140523', 'pm_score': 2, 'selected': False, 'text': "<p>I generated the following using python and works with the re module. The look-ahead assertions ensure that the correct number of dots or colons appear in the address.  It does not support IPv4 in IPv6 notation.</p>\n\n<pre><code>pattern = '^(?=\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$)(?:(?:25[0-5]|[12][0-4][0-9]|1[5-9][0-9]|[1-9]?[0-9])\\.?){4}$|(?=^(?:[0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4}$)(?![^:]*::.+::[^:]*$)(?:(?=.*::.*)|(?=\\w+:\\w+:\\w+:\\w+:\\w+:\\w+:\\w+:\\w+))(?:(?:^|:)(?:[0-9a-f]{4}|[1-9a-f][0-9a-f]{0,3})){0,8}(?:::(?:[0-9a-f]{1,4}(?:$|:)){0,6})?$'\nresult = re.match(pattern, ip)\nif result: result.group(0)\n</code></pre>\n"}, {'answer_id': 43307289, 'author': 'Master James', 'author_id': 4928388, 'author_profile': 'https://Stackoverflow.com/users/4928388', 'pm_score': 0, 'selected': False, 'text': "<p>Just matching local ones from an origin with square brackets included. I know it's not as comprehensive but in javascript the other ones had difficult to trace issues primarily that of not working, so this seems to get me what I needed for now. extra capitals A-F aren't needed either.</p>\n\n<pre><code>^\\[([0-9a-fA-F]{1,4})(\\:{1,2})([0-9a-fA-F]{1,4})(\\:{1,2})([0-9a-fA-F]{1,4})(\\:{1,2})([0-9a-fA-F]{1,4})(\\:{1,2})([0-9a-fA-F]{1,4})\\]\n</code></pre>\n\n<p>Jinnko's version is simplified and better I see.</p>\n"}, {'answer_id': 45271143, 'author': 'Alexandre Fenyo', 'author_id': 8334991, 'author_profile': 'https://Stackoverflow.com/users/8334991', 'pm_score': 0, 'selected': False, 'text': '<p>As stated above, another way to get an IPv6 textual representation <strong>validating</strong> parser is to use programming. Here is one that is fully compliant with RFC-4291 and RFC-5952. I\'ve written this code in ANSI C (works with GCC, passed tests on Linux - works with clang, passed tests on FreeBSD). Thus, it does only rely on the ANSI C standard library, so it can be compiled everywhere (I\'ve used it for IPv6 parsing inside a kernel module with FreeBSD).</p>\n\n<pre><code>// IPv6 textual representation validating parser fully compliant with RFC-4291 and RFC-5952\n// BSD-licensed / Copyright 2015-2017 Alexandre Fenyo\n\n#include &lt;string.h&gt;\n#include &lt;netinet/in.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;ctype.h&gt;\n\ntypedef enum { false, true } bool;\n\nstatic const char hexdigits[] = "0123456789abcdef";\nstatic int digit2int(const char digit) {\n  return strchr(hexdigits, digit) - hexdigits;\n}\n\n// This IPv6 address parser handles any valid textual representation according to RFC-4291 and RFC-5952.\n// Other representations will return -1.\n//\n// note that str input parameter has been modified when the function call returns\n//\n// parse_ipv6(char *str, struct in6_addr *retaddr)\n// parse textual representation of IPv6 addresses\n// str:     input arg\n// retaddr: output arg\nint parse_ipv6(char *str, struct in6_addr *retaddr) {\n  bool compressed_field_found = false;\n  unsigned char *_retaddr = (unsigned char *) retaddr;\n  char *_str = str;\n  char *delim;\n\n  bzero((void *) retaddr, sizeof(struct in6_addr));\n  if (!strlen(str) || strchr(str, \':\') == NULL || (str[0] == \':\' &amp;&amp; str[1] != \':\') ||\n      (strlen(str) &gt;= 2 &amp;&amp; str[strlen(str) - 1] == \':\' &amp;&amp; str[strlen(str) - 2] != \':\')) return -1;\n\n  // convert transitional to standard textual representation\n  if (strchr(str, \'.\')) {\n    int ipv4bytes[4];\n    char *curp = strrchr(str, \':\');\n    if (curp == NULL) return -1;\n    char *_curp = ++curp;\n    int i;\n    for (i = 0; i &lt; 4; i++) {\n      char *nextsep = strchr(_curp, \'.\');\n      if (_curp[0] == \'0\' || (i &lt; 3 &amp;&amp; nextsep == NULL) || (i == 3 &amp;&amp; nextsep != NULL)) return -1;\n      if (nextsep != NULL) *nextsep = 0;\n      int j;\n      for (j = 0; j &lt; strlen(_curp); j++) if (_curp[j] &lt; \'0\' || _curp[j] &gt; \'9\') return -1;\n      if (strlen(_curp) &gt; 3) return -1;\n      const long val = strtol(_curp, NULL, 10);\n      if (val &lt; 0 || val &gt; 255) return -1;\n      ipv4bytes[i] = val;\n      _curp = nextsep + 1;\n    }\n    sprintf(curp, "%x%02x:%x%02x", ipv4bytes[0], ipv4bytes[1], ipv4bytes[2], ipv4bytes[3]);\n  }\n\n  // parse standard textual representation\n  do {\n    if ((delim = strchr(_str, \':\')) == _str || (delim == NULL &amp;&amp; !strlen(_str))) {\n      if (delim == str) _str++;\n      else if (delim == NULL) return 0;\n      else {\n        if (compressed_field_found == true) return -1;\n        if (delim == str + strlen(str) - 1 &amp;&amp; _retaddr != (unsigned char *) (retaddr + 1)) return 0;\n        compressed_field_found = true;\n        _str++;\n        int cnt = 0;\n        char *__str;\n        for (__str = _str; *__str; ) if (*(__str++) == \':\') cnt++;\n        unsigned char *__retaddr = - 2 * ++cnt + (unsigned char *) (retaddr + 1);\n        if (__retaddr &lt;= _retaddr) return -1;\n        _retaddr = __retaddr;\n      }\n    } else {\n      char hexnum[4] = "0000";\n      if (delim == NULL) delim = str + strlen(str);\n      if (delim - _str &gt; 4) return -1;\n      int i;\n      for (i = 0; i &lt; delim - _str; i++)\n        if (!isxdigit(_str[i])) return -1;\n        else hexnum[4 - (delim - _str) + i] = tolower(_str[i]);\n      _str = delim + 1;\n      *(_retaddr++) = (digit2int(hexnum[0]) &lt;&lt; 4) + digit2int(hexnum[1]);\n      *(_retaddr++) = (digit2int(hexnum[2]) &lt;&lt; 4) + digit2int(hexnum[3]);\n    }\n  } while (_str &lt; str + strlen(str));\n  return 0;\n}\n</code></pre>\n'}, {'answer_id': 47861266, 'author': 'Sean F', 'author_id': 6801443, 'author_profile': 'https://Stackoverflow.com/users/6801443', 'pm_score': 2, 'selected': False, 'text': '<p>Regexes for ipv6 can get really tricky when you consider addresses with embedded ipv4 and addresses that are compressed, as you can see from some of these answers.</p>\n\n<p><a href="https://seancfoley.github.io/IPAddress/" rel="nofollow noreferrer">The open-source IPAddress Java library</a> will validate all standard representations of IPv6 and IPv4 and also supports prefix-length (and validation of such).  Disclaimer: I am the project manager of that library.</p>\n\n<p>Code example:</p>\n\n<pre><code>        try {\n            IPAddressString str = new IPAddressString("::1");\n            IPAddress addr = str.toAddress();\n            if(addr.isIPv6() || addr.isIPv6Convertible()) {\n                IPv6Address ipv6Addr = addr.toIPv6();\n            }\n            //use address\n        } catch(AddressStringException e) {\n            //e.getMessage has validation error\n        }\n</code></pre>\n'}, {'answer_id': 50385461, 'author': 'Jitendra Gosavi', 'author_id': 5725705, 'author_profile': 'https://Stackoverflow.com/users/5725705', 'pm_score': 3, 'selected': False, 'text': '<p>Following regex is for IPv6 only. Group 1 matches with the IP.</p>\n\n<pre><code>(([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4})\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/53497', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4883/']