Regex bug on Mono
This is a good advice for all creating applications using .NET that need to work on both Microsoft .NET and Mono.
It seems that Mono ignores group indexes specified explicitly.
Here’s the code to reproduce this problem.
The results on Mono are incorrect.
Regex parser = new Regex("(?<2>.*?),(?<1>.*?),"); Match match = parser.Match("first,second,"); Console.WriteLine("Groups[1].Value={0}", match.Groups[1].Value); Console.WriteLine("Groups[2].Value={0}", match.Groups[2].Value);
MS implementation:
C:\1\ConsoleApplication\1\bin\Debug>ConsoleApplication1.exe Groups[1].Value=second Groups[2].Value=first
Mono 2.4.2.3 implementation:
C:\1\ConsoleApplication\1\bin\Debug>mono.exe ConsoleApplication1.exe Groups[1].Value=first Groups[2].Value=second
The workaround is simple use names instead of numbers:
Regex parser = new Regex("(?<first>.*?),(?<second>.*?),"); Match match = parser.Match("first,second,"); Console.WriteLine("Groups[first].Value={0}", match.Groups["first"].Value); Console.WriteLine("Groups[second].Value={0}", match.Groups["second"].Value);