re = /^(?!.*::.*::)fe80(?=(?::+[^:]+){0,6}$)(?:(:0{1,4})*|(?1){3,}(?::+[^:]+)):(?=:)(:[0-9a-f]{1,4}){0,4}$|^fe80(?=(?::[^:]+){7}$)(?1){3,}(?2){0,4}$/mi
str = 'valid cases:
FE80::1
FE80::1:0:1
FE80::0000:1
FE80:0::1
FE80:000::1
FE80:0:00::1
FE80:0:00:000::1
FE80:0:00:000:0000::1
fe80::9656:d028:8652:66b6
fe80:0000:0000:0000:0000:0000:0000:0000
fe80:00:000:0000:1234:5678:ABCD:EF
fe80:00:000:0000:1234:5678:ABCD:00
fe80::1234:5678:ABCD:EF
fe80:00:000:0000::EF
fe80:00::ABCD:EF
FE80:0:00:000:0001::1
FE80:0:00:000:0000:1::1
invalid cases:
fe80:66b6:9656:d028:8652:66b6:9656:d028:0000
fe80:66b6:9656:d028:8652:66b6:9656:d028
fe80:1:000:0000:1234:5678:ABCD:EF:9999
fe80::bad:1234:5678:ABCD:EF
fe80:0:bad::EF
fe80:1:2:3:4:6:5:7
fe80:0:bad:EF
Fe90::1
2000::1
fe80:00:000:0000:0:0:0::EF
fe80:0:0:0:ABCD:EF:0
FE80:0:00:001:0001::1
'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html