Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| // <Snippet4> | |
| using System; | |
| using System.Diagnostics; | |
| using System.Text.RegularExpressions; | |
| public class Example | |
| { | |
| public static void Main() | |
| { | |
| string input = "b51:4:1DB:9EE1:5:27d60:f44:D4:cd:E:5:0A5:4a:D24:41Ad:"; | |
| bool matched; | |
| Stopwatch sw; | |
| Console.WriteLine("With backtracking:"); | |
| string backPattern = "^(([0-9a-fA-F]{1,4}:)*([0-9a-fA-F]{1,4}))*(::)$"; | |
| sw = Stopwatch.StartNew(); | |
| matched = Regex.IsMatch(input, backPattern); | |
| sw.Stop(); | |
| Console.WriteLine("Match: {0} in {1}", Regex.IsMatch(input, backPattern), sw.Elapsed); | |
| Console.WriteLine(); | |
| Console.WriteLine("Without backtracking:"); | |
| string noBackPattern = "^((?>[0-9a-fA-F]{1,4}:)*(?>[0-9a-fA-F]{1,4}))*(::)$"; | |
| sw = Stopwatch.StartNew(); | |
| matched = Regex.IsMatch(input, noBackPattern); | |
| sw.Stop(); | |
| Console.WriteLine("Match: {0} in {1}", Regex.IsMatch(input, noBackPattern), sw.Elapsed); | |
| } | |
| } | |
| // The example displays output like the following: | |
| // With backtracking: | |
| // Match: False in 00:00:27.4282019 | |
| // | |
| // Without backtracking: | |
| // Match: False in 00:00:00.0001391 | |
| // </Snippet4> |