Is there any way to parse a Complex RegEx pattern(containing several named groups as well as several numbered groups and non-capturing groups) and report about each groupname or groupnumber along with pattern text.
Suppose, I do have a RegEx pattern like this:
(?im)(?<x>\b[a-s03]+\b)(?-i)(?<a>\p{L}+?,(?<b>.+?:(?<c>.+?;(?<d>.+?(?:\d|sample-text|(\k'x'|sos30))))))
And I like to extract:=
Named groups:
x==>(?<x>\b[a-s03]+\b)
a==>(?<a>\p{L}+?,(?<b>.+?:(?<c>.+?;(?<d>.+?(?:\d|sample-text|(\k'x'|sos30))))))
b==>(?<b>.+?:(?<c>.+?;(?<d>.+?(?:\d|sample-text|(\k'x'|sos30)))))
c==>(?<c>.+?;(?<d>.+?(?:\d|sample-text|(\k'x'|sos30))))
d==>(?<d>.+?(?:\d|sample-text|(\k'x'|sos30)))
Numbered groups:
1==>(\k'x'|sos30)
Non-capturing-groups:
1st==>(?:\d|sample-text|(\k'x'|sos30))
Purpose of this Requirement:
I do have a large database of complex RegEx patterns. The previous programmar worked on this did not use any comment [(?#...)] while preparing these complex patterns, moreover no linebreaks exists within those patterns. I have to modify those patterns some cases and also have to use comment within those patterns. Now it is something like searching a needle in the haystakes. I simply could not use RegEx for this purpose. So, I inclined to use a parser for this case.
What I tried:
I tried GetGroupNames and GetGroupNumbers collection for that purpose. I could extract only the Names/Numbers of the groups, but not the corresponding textual patterns.
I am looking for a Non-RegEx solution/some hints.
How about this, for this:
This, as the Output:
This is the code:
The only difference is that I’m not capturing either Non-Capture groups, nor function groups. Of course, this is just quick code I made in like 10 minutes. But it’s a start if you want it. I use the OrderedDictionary as Keys for Group-Numbers. You could change that structure if you wanted to also include non-capture groups and function groups in the output.