Insert a Balancing Group

The Insert Token button on the Create panel makes it easy to insert a balancing group. Only the .NET regex engine and the Just Great Software applications support this.

You need to add one or more numbered or named capturing groups to your regular expression before you can add a balancing group. When you select the Balancing Group item in the Insert Token menu, a window pops up. This lets you select an existing capturing group that the balancing group will subtract from. This is required. If you want, you can also give the balancing group a name of its own.

In the regex ^(?<open>o)+(?<close-open>c)+$, the first group is a capturing group with the name “open”. It captures the letter o with each iteration of the group. The second group is a balancing group. Each time the balancing group matches the letter c, it subtracts one match from the group “open”. If the group “open” does not have any matches or if all its matches were already subtracted then the balancing group fails to match, even if there are more letters c in the subject string. That’s why it is called a “balancing” group. This regex matches a “balanced construct” of one or more letters o followed by an equal number of letters c. The balancing group itself has the name “close”. Each time the balancing group subtracts a match from the group “open”, it captures the text between the match that it just subtracted and the balancing group’s own match. So after this regex has matched oooccc, the group “open” holds no matches (because they were all subtracted) and the group “close” holds oocc as its final match.

Insert a balancing group