
Most other regex engines only store the most recent match of each capturing groups. NET keep a stack of everything they captured during the matching process that wasn’t backtracked or subtracted.

If the balancing group succeeds and it has a name (“capture” in this example), then the group captures the text between the end of the match that was subtracted from the group “subtract” and the start of the match of the balancing group itself (“regex” in this example). The difference is that the balancing group has the added feature of subtracting one match from the group “subtract”, while a conditional leaves the group untouched. You could think of a balancing group as a conditional that tests the group “subtract”, with “regex” as the “if” part and an “else” part that always fails to match. If the group “subtract” did not match yet, or if all its matches were already subtracted, then the balancing group fails to match. When the regex engine enters the balancing group, it subtracts one match from the group “subtract”. The name “subtract” must be the name of another group in the regex. (?regex) or (?'-subtract'regex) is the syntax for a non-capturing balancing group. NET but with two group names delimited by a minus sign. It’s the same syntax used for named capturing groups in. (?regex) or (?'capture-subtract'regex) is the basic syntax of a balancing group. JGsoft V2 supports both balancing groups and recursion. NET’s solution to a problem that other regex flavors like Perl, PCRE, and Ruby handle with regular expression recursion. A technically more accurate name for the feature would be capturing group subtraction. The main purpose of balancing groups is to match balanced constructs or nested constructs, which is where they get their name from.

NET regex flavor has a special feature called balancing groups. Matching Nested Constructs with Balancing Groups
