using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^(?!.*::.*::)fe80(?=(?::+[^:]+){0,6}$)(?:(:0{1,4})*|(?1){3,}(?::+[^:]+)):(?=:)(:[0-9a-f]{1,4}){0,4}$|^fe80(?=(?::[^:]+){7}$)(?1){3,}(?2){0,4}$";
string input = @"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
";
RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
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 C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx