I have a C# Regex class matching multiple subgroups such as
(?<g1>abc)|(?<g2>def)|(?<g3>ghi)
but with much more complicated sub-patterns. I basically want to match anything that doesn’t belong to any of those groups, in addition to existing groups.
I tried
(?<g1>abc)|(?<g2>def)|(?<g3>ghi)|(.+?)
but it turned out too slow. I can’t do negation because I don’t want to copy those complex subpatterns redundantly. Using just (.+) overrides all other groups as expected.
Is there any other way? If that doesn’t work I’ll have to write an ad-hoc parser.
Additional details: All these groups are evaluated against a MatchEvaluator. So a Regex class behavior that sends ‘unmatched strings’ to the MatchEvaluator will also work.
A sample text would be
.......abc........ghi.....def.....abc....def...ghi......abc.......
I want to catch parts inbetween.
Your regex generates separate match for every single character outside g1,g2,g3. So when you use it with MatchEvaluator it generates lots of evaluator calls. Thats why its slow.
If you try following regex:
you will get single ‘rest’ group match for entire fragment of text that doesnt contain ‘g’ group.
Regex C# code: