Kovpaev Alexey Kovpaev Alexey - 4 months ago 17
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:

System.Action[Int32,Dictionary[Int32,Int32],Int32]


lets assume type name is
[\w.]+
and parameter is
[\w.,\[\]]+

so I need to grab only
Int32
,
Dictionary[Int32,Int32]
and
Int32


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

Answer

I suggest capturing those values using

\w+(?:\.\w+)*\[(?:,?(?<res>\w+(?:\[[^][]*])?))*

See the regex demo.

Details:

  • \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)
        .Cast<Match>()
        .SelectMany(x => x.Groups["res"].Captures.Cast<Capture>()
            .Select(t => t.Value))
        .ToList();
foreach (var s in result) // DEMO
    Console.WriteLine(s);
Comments