Kovpaev Alexey Kovpaev Alexey - 1 year ago 185
C# Question

Regex with balancing groups

I need to write regex that capture generic arguments (that also can be generic) of type name in special notation like this:


lets assume type name is
and parameter is

so I need to grab only

Basically I need to take something if balancing group stack is empty, but I don't really understand how.

Answer Source

I suggest capturing those values using


See the regex demo.


  • \w+(?:\.\w+)* - match 1+ word chars followed with . + 1+ word chars 1 or more times
  • \[ - a literal [
  • (?:,?(?<res>\w+(?:\[[^][]*])?))* - 0 or more sequences of:
    • ,? - an optional comma
    • (?<res>\w+(?:\[[^][]*])?) - Group "res" capturing:
      • \w+ - one or more word chars (perhaps, you would like [\w.]+)
      • (?:\[[^][]*])? - 1 or 0 (change ? to * to match 1 or more) sequences of a [, 0+ chars other than [ and ], and a closing ].

A C# demo below:

var line = "System.Action[Int32,Dictionary[Int32,Int32],Int32]";
var pattern = @"\w+(?:\.\w+)*\[(?:,?(?<res>\w+(?:\[[^][]*])?))*";
var result = Regex.Matches(line, pattern)
        .SelectMany(x => x.Groups["res"].Captures.Cast<Capture>()
            .Select(t => t.Value))
foreach (var s in result) // DEMO
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download