Last update: 2017-12-04
PCRE
118(+3) characters
/^(((?=.*(::))(?!.*\3.+\3))\3?|([\dA-F]{1,4}(\3|:\b|$)|\2))(?4){5}((?4){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})\z/i
Perl
118(+4) characters
/^(((?=.*(::))(?!.*\3.+\3))\3?|([\dA-F]{1,4}(\3|:\b|$)|\2))(?4){5}((?4){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})\z/ai
Java
148 characters (unescaped, needs i-flag, explanation, ::-detection part is moved because of a Java regex-engine bug)
^([\dA-F]{1,4}:|((?=.*(::))(?!.*\3.+\3))\3?)([\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
Ruby
148(+3) characters
/^(((?=.*(::))(?!.*\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
Phython
148 characters (needs i-flag & s-flag but no u-flag)
^(((?=.*(::))(?!.*\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
.NET
157 characters (needs i-flag)
^(((?=.*(::))(?!.*\3.+\3))\3?|[0-9A-F]{1,4}:)([0-9A-F]{1,4}(\3|:\b)|\2){5}(([0-9A-F]{1,4}(\3|:\b|$)|\2){2}|(((2[0-4]|1[0-9]|[1-9])?[0-9]|25[0-5])\.?\b){4})\z
ECMA (JavaScript)
161(+3) characters
/^((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\3)::|:\b|$))|(?!\2\3)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i
XML
408 characters
([0-9A-Fa-f]{1,4}:([0-9A-Fa-f]{1,4}:([0-9A-Fa-f]{1,4}:([0-9A-Fa-f]{1,4}:([0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{0,4}|:[0-9A-Fa-f]{1,4})?|(:[0-9A-Fa-f]{1,4}){0,2})|(:[0-9A-Fa-f]{1,4}){0,3})|(:[0-9A-Fa-f]{1,4}){0,4})|:(:[0-9A-Fa-f]{1,4}){0,5})((:[0-9A-Fa-f]{1,4}){2}|:(25[0-5]|(2[0-4]|1[0-9]|[1-9])?[0-9])(\.(25[0-5]|(2[0-4]|1[0-9]|[1-9])?[0-9])){3})|(([0-9A-Fa-f]{1,4}:){1,6}|:):[0-9A-Fa-f]{0,4}|([0-9A-Fa-f]{1,4}:){7}:
Test data
Valid IPv6 addresses Invalid IPv6 addresses
Info
Total number of valid IPv6 addresses (case-sensitive)
IPv4 segment speed optimalization
48 possible configurations
Tests performed to determine best configuration
RegEx Tricks/Tips
Convert Atomic-Group to Positive-Lookahead & Backreference (ECMA/Python/TCL ARE)
(?>regex)
(?=(regex))\1
Convert *+ to Atomic group (.NET/Perl/Ruby)*
regex*+
(?>regex*)
Convert ++ to Atomic group (.NET/Perl/Ruby)*
regex++
(?>regex+)
Convert ?+ to Atomic group (.NET/Perl/Ruby)*
regex?+
(?>regex?)
Convert {n,m}+ to Atomic group (.NET/Perl/Ruby)*
regex{n,m}+
(?>regex{n,m})
Convert *? to alternation (XML)
regex*?
(|regex)*
Convert +? to alternation (XML)
regex+?
regex(|regex)*
Convert ?? to alternation (XML)
regex??
(|regex)
Convert {n,}? to alternation (XML)
regex{n,}?
regex{n}(|regex)*
Convert {n,m}? to alternation (XML)
regex{n,m}?
regex{n}(|regex){m-n}
Convert conditional to Lookaround (JAVA/ECMA/Python/Ruby/Tcl ARE)
(?(?=regex)then|else)
(?=regex)then|(?!regex)else
(?(?!regex)then|else)
(?!regex)then|(?=regex)else
Convert conditional to Lookaround (JAVA/Python)
(?(?<=regex)then|else)
(?<=regex)then|(?<!regex)else
(?(?<!regex)then|else)
(?<!regex)then|(?<=regex)else
Convert comment to Positive-Lookahead & alternation (JAVA)**
(?#comment)
(?=|\Qcomment\E)
Convert comment to Positive-Lookahead & alternation (JAVA/ECMA)***
(?#comment)
(?=|comment)
Convert ? to {n,m} (POSIX BRE)
regex?
regex\{0,1\}
Convert + to {n,} (POSIX BRE)
regex+
regex\{1,\}
Force capture group to be non-true/empty (Java/Perl/PCRE/ECMA/Python/Ruby/Tcl ARE)****
$(?!\1) [End of regex]
(?!.*$\1) [In the regex]
* = Also usable for ECMA/Phyton/TCL ARE when converting the Atomic-Group to Positive-Lookahead & Backreference
** = \E is not allowed as part of the comment
*** = Meta characters may be needed to be escaped
**** = Capture groups containing only a newline token might cause problems.
Email
my($email) = "freak&er56@&hotma&il.com"; $email =~ s/&//g
License
WTFPL v2