EN VI

Why Regex in a while loop will match only the first occurrence len(is not dynamic in a while loop)

2022-12-25 22:59:51

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 ?

Solution:

In your first approach, you are only executing regex.Match once, so you are always looking at the very same match until your code throws an Exception. Depending on whether your first match is at index 0 or later, it's an OutOfBounds exception (because you try to remove from an empty string) or an OutOfMemory exception (because you are removing nothing from your string but adding to your result list indefinitively.

Your second approach will suffer from the same OutOfMemory exception if your input starts with a 0 or you arrive at some intermediate result string which starts with 0

See below for a working approach:

List<string> ZerosMatch(string input)
{
    var newInput = input;
    var list = new List<string>();
    var regex = new Regex(@"[0]{1,}");
    var match = regex.Match(newInput);
    while (match.Success)
    {
        newInput = newInput.Remove(match.Index, match.Value.Length);
        list.Add(match.Value);
        match = regex.Match(newInput);
    }
    return list;
}
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login