❔ 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.
error is an object that contains the regex, solution, and unique ID.
errorData is a list of said errors.
20 Replies
Are you looking for a Regex solution?
Well essentially instead of a string.Replace I want to do a
string.Format(error.Solution, match.Groups)
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?
Yes exactly
Are the errors generic?
Do they have a type?
There are no errors
This current method works, but i'd rather use parameters than a string.replace
I mean the objects
Are the objects generic
Oh my bad sorry
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
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
.. :/
okay so, the object you're describing is the complete information
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
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?
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}.
and this is incorrect?
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."
ah so it's more or less a regex problem
Sorry, just trying to reduce it
Is it? I guess that would make more sense
1. What is your regex pattern that's failing?
2. What's the input string?
3. What's the expected output?
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.