❔ Convert regex match groups into parameters

So I have a program that is checking for common errors in a log, it uses regex to capture them, then also groups some information in them.
I then match them up with a solution which has some {#} parameters which I replace with the groups. Right now im using a gross string.Replace() method which is being inconsistent so i'd rather use the parameters correctly, issue is I don't know how many parameters or groups there are depending on the match.

        foreach (var error in errorData)
        {
            var errregex = new Regex(error.Regex);
            var errmatch = errregex.Match(wholeLog);
            if (errmatch.Success)
            {
                
                for (var i = 0; i <= 10; i++)
                {
                    error.Solution = error.Solution.Replace("{" + i + "}", errmatch.Groups[i].Value);
                }
                if (log.Errors.All(x => x.ID != error.ID)) log.Errors.Add(error);
            }
        }


error is an object that contains the regex, solution, and unique ID.
errorData is a list of said errors.
Was this page helpful?