I have a regex which I would imagine dynamically captures my group of zeros. What happens is I get a list full of e.g. [00, 00, 00, 00, 00] from a string like "001111110000001100110011111"
I've tried putting my var regex = new Regex() inside the while loop in hopes this might solve my problem. Whatever I try, regex returns only the first occurrences' length of zeros instead of filling my collection with varying zeros amounts.
List<string> ZerosMatch(string input)
{
var newInput = input;
var list = new List<string>();
var regex = new Regex(@"[0]{1,}");
var matches = regex.Match(newInput);
while (matches.Success)
{
list.Add(matches.Value);
try
{
newInput = newInput.Remove(0, matches.Index);
}
catch
{
break;
}
}
return list;
}
vs
List<string> ZerosMatch(string input)
{
var newInput = input;
var list = new List<string>();
bool hasMatch = true;
while (hasMatch)
{
try
{
var regex = new Regex(@"[0]{1,}");
var matches = regex.Match(newInput);
newInput = newInput.Remove(0, matches.Index);
list.Add(matches.Value);
hasMatch = matches.Success;
}
catch
{
break;
}
}
return list;
}
My question is Why is this happening ?