It is not clear to me whether this will be a mistake or not, but if you change .* To .+ , It will do what you want. I suspect that (.*) Matches an empty string that confuses things.
It is supported by the following code:
using System; using System.Text.RegularExpressions; class Test { static void Main() { var match = Regex.Match("abc", "(.*)"); while (match.Success) { Console.WriteLine(match.Length); match = match.NextMatch(); } } }
This prints 3, then 0. Changing the pattern to "(.+)" Makes it easy to print 3.
It should be noted that this has nothing to do with C # as a language - only standard .NET libraries. It is worth distinguishing between language and libraries - for example, you will get exactly the same behavior if you use the standard .NET library from F #, VB, C ++ / CLI, etc.
source share