I have the following strings I need to parse:
<!-- MOCK -->
<!--MOCK-->
<!-- MOCK:SENT;FAIL -->
<!--MOCK:SENT;SUCCESS-->
<!--MOCK:SENT; SUCCESS-->
<!--MOCK:SENT ; SUCCESS-->
<!--MOCK:SENT -->
<!-- MOCK:SENT-->
<!-- MOCK : SENT -->
<!-- MOCK: SENT -->
<!-- MOCK :SENT -->
Because these may be entered by a person I want to provide, within reason, support for various spacing.
I've gotten as far as the following regular expression.
<!-- *(?<mode>.*?(?=[ \-:])).*: *(?<responses>.*?(?= *-->))
This works for all of the above test strings except the first two. In those first two cases the "mode" group comes back empty, which I don't understand because I thought the ?<mode>.*?(?=[ \-:]) would collect all characters until it came upon a space, dash, or colon. If I change .*: * in the middle to .*:? * then I get all the "mode" values but I lose all of my "responses" values.
My test code is in .NET Fiddle (https://dotnetfiddle.net/HzIXch), but here it is for completeness.
var regex = new Regex(@"<!-- *(?<mode>.*?(?=[ \-:])).*: *(?<responses>.*?(?= *-->))");
var values = new[] {
"<!-- MOCK -->",
"<!--MOCK-->",
"<!-- MOCK:SENT;FAIL -->",
"<!--MOCK:SENT;SUCCESS-->",
"<!--MOCK:SENT; SUCCESS-->",
"<!--MOCK:SENT ; SUCCESS-->",
"<!--MOCK:SENT -->",
"<!-- MOCK:SENT-->",
"<!-- MOCK : SENT -->",
"<!-- MOCK: SENT -->",
"<!-- MOCK :SENT -->",
};
foreach (var value in values)
{
var match = regex.Match(value);
Console.WriteLine($"Mode: [{match.Groups["mode"].Value}], Responses: [{match.Groups["responses"].Value}]");
}