Insert a Subroutine Call

The Insert Token button on the Create panel makes it easy to insert tokens that make a subroutine call to a capturing group. Only a few regex engines such as Perl, PCRE, and Ruby support this.

If you’ve added one or more numbered or named capturing groups to your regular expression then you can make that group recurse into itself. (?1) or \g<1> recurses into a numbered group, while (?&name) or \g<name> recurses into a named group. The regex \A(\([^()]*+(?:(?1)[^()]*+)*+\))\z matches a pair of properly nested parentheses in the same way the example in the previous section does, but adds anchors to make the regex match the whole string (or not at all). The anchors need to be excluded from the recursion, which we do by adding a capturing group and limiting the recursion to the capturing group.

You can use the same syntax to insert a subroutine call to a named or numbered capturing group. (\d++)\+(?1)=(?1) is equivalent to (\d++)\+(?:\d++)=(?:\d++) and matches something like 1+2=3. This illustrates the key difference between a subroutine call and a backreference. A backreference matches the exact same text that was most recently matched by the group. A subroutine call reuses the part of the regex inside the group. Subroutine calls can significantly increase the readability and reduce the complexity of regular expressions that need to match the same construct (but not the exact same text) in more than one place. If we extend these two regex to match sums of floating point numbers in scientific notation, they become ([0-9]*+\.?+[0-9]++([eE][-+]?+[0-9]++)?+)\+(?1)=(?1) and ([0-9]*+\.?+[0-9]++([eE][-+]?+[0-9]++)?+)\+(?:[0-9]*+\.?+[0-9]++([eE][-+]?+[0-9]++)?+)=(?:[0-9]*+\.?+[0-9]++([eE][-+]?+[0-9]++)?+).

To get the correct syntax for your application, select Subroutine Call in the Insert Token menu. In the window that appears, click inside the capturing group to which you want to insert a subroutine call. RegexBuddy automatically inserts a named subroutine call when you select a named group, and a numbered subroutine call when you select a numbered group.

The window also has options for how the subroutine call should handle backtracking and capturing groups. This behavior differs between applications. It even differs between different versions of Perl and different versions of PCRE2.

In the Just Great Software applications, subroutine calls behave the way they originally did in Perl, PCRE, and Ruby, depending on whether you use the syntax introduced by Perl, PCRE, or Ruby. If you’re targeting one of those applications then you can select the behavior you want and RegexBuddy inserts the appropriate syntax. Other applications only have a single behavior, even if they support various syntax. Then the behavior options on the Insert Subroutine Call are merely informative.

The differences don’t affect any of the examples on this page. They only use possessive quantifiers, which never backtrack. They don’t have any capturing groups inside the subroutine calls.

Insert a subroutine call