❔ 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
Sweerpotato8mo 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
Sweerpotato8mo 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
Sweerpotato8mo 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
Sweerpotato8mo 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
Sweerpotato8mo 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
Sweerpotato8mo 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
Sweerpotato8mo 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
Sweerpotato8mo 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
Sweerpotato8mo 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
Sweerpotato8mo ago
1. What is your regex pattern that's failing? 2. What's the input string? 3. What's the expected output?
Accord
Accord8mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.