❔ 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);
}
}
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.
20 Replies
Sweerpotato
Sweerpotato15mo ago
Are you looking for a Regex solution?
๖̶̶̶ζ͜͡SuperPyroManiac
Well essentially instead of a string.Replace I want to do a string.Format(error.Solution, match.Groups)
Sweerpotato
Sweerpotato15mo ago
Okay, so you already have a regex solution, but you would like to migrate that to some sort of parameterized solution if I understand the problem?
๖̶̶̶ζ͜͡SuperPyroManiac
Yes exactly
Sweerpotato
Sweerpotato15mo ago
Are the errors generic? Do they have a type?
๖̶̶̶ζ͜͡SuperPyroManiac
There are no errors This current method works, but i'd rather use parameters than a string.replace
Sweerpotato
Sweerpotato15mo ago
I mean the objects Are the objects generic
๖̶̶̶ζ͜͡SuperPyroManiac
Oh my bad sorry
public class Error
{
public int ID { get; set; }
public string Regex { get; set; }
public string Solution { get; set; }
public string Level { get; set; }
}
public class Error
{
public int ID { get; set; }
public string Regex { get; set; }
public string Solution { get; set; }
public string Level { get; set; }
}
Sweerpotato
Sweerpotato15mo ago
Okay so first of all, why does the error object contain a regex? Just asking poking questions, it's not a critique It will lead us to a more sound structure
๖̶̶̶ζ͜͡SuperPyroManiac
Regex is stored on my SQL database, when needed I can create a list of all the errors on the regex server including the solution and level of severity So each "error" is bundled with everything I would need
Sweerpotato
Sweerpotato15mo ago
.. :/ okay so, the object you're describing is the complete information
๖̶̶̶ζ͜͡SuperPyroManiac
Yeah It's a discord bot, it reads logs and checks for common errors We store a bunch of common errors in the database, and this bot will use that information The solution has params like {1}, {2} in some cases, so at the part where I find the errors I want to change those to the groups that match The current method of error.Solution = error.Solution.Replace("{" + i + "}", errmatch.Groups[i].Value); Works, but sometimes it just.. Doesnt for whatever reason
Sweerpotato
Sweerpotato15mo ago
My gut feeling is this: you want to remove Regex from your database structure Make a function that receives an Error object and returns a string Differentiate error objects with an id somehow if you need to use different regular expressions Process the Error object inside this function If the regex doesn't work it's probably a grouping problem Can you write concrete cases where it fails and what regex you're using?
๖̶̶̶ζ͜͡SuperPyroManiac
So this one for example Reg .+Version: RAGE Plugin Hook (?!v1\.106\.1330\.16514).* Solution Update RPH using the manual LSPDFR installation. You are currently using version: {1}.
Sweerpotato
Sweerpotato15mo ago
and this is incorrect?
๖̶̶̶ζ͜͡SuperPyroManiac
solution for this one will return "Update RPH using the manual LSPDFR installation. You are currently using version: ." Instead of "Update RPH using the manual LSPDFR installation. You are currently using version: TheirCurrentVersion."
Sweerpotato
Sweerpotato15mo ago
ah so it's more or less a regex problem Sorry, just trying to reduce it
๖̶̶̶ζ͜͡SuperPyroManiac
Is it? I guess that would make more sense
Sweerpotato
Sweerpotato15mo ago
1. What is your regex pattern that's failing? 2. What's the input string? 3. What's the expected output?
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?