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1-9]?\d)(.(25[0-5]¦2[0-4]\d¦1\d\d¦[1-9]?\d)){3}))¦:)))(%.+)?\s*$/

该正则能够匹配如下形式的IPV6地址

fe80:0000:0000:0000:0204:61ff:fe9d:f156
fe80:0:0:0:204:61ff:fe9d:f156
fe80::204:61ff:fe9d:f156
fe80:0000:0000:0000:0204:61ff:254.157.241.86
fe80:0:0:0:0204:61ff:254.157.241.86
fe80::204:61ff:254.157.241.86

::1
fe80::
2001::

转自http://forums.intermapper.com/viewtopic.php?t=452

IPv6的正则表达式
Share this