Feature: Subroutines
Home | Features | Improve this section
A Subroutine is a pre-defined capture group or named capture group that can be reused in multiple places within the pattern to re-evaluate the subexpression from the group.
Syntax
NOTE: The following syntax is an example based on some of the supported engines. For specific engine support, see Engines.
(?(DEFINE)…)— Defines a set of reusable capture groups that can be referenced elsewhere in the pattern.(?n)— Where n is an integer >= 1. Evaluates the capture group whose offset is n.(?-n)— Where n is an integer >= 1. Evaluates the capture group whose offset is the nth capture group declared to the left of the current Atom. Example:(?-1)would revaluate the last declared capture group.(?+n)— Where n is an integer >= 1. Evaluates the capture group whose offset is the nth capture group declared to the right of the current Atom. Example:(?+1)would evaluate the next declared capture group.(?&name)— Evaluates the named capture group with the provided name.
Example
(?(DEFINE)
(?<Year>\d{4}|[+-]\d{5,})
(?<Month>0[1-9]|1[0-2])
(?<Day>0[1-9]|2[0-9]|3[01])
)
(?<Date>(?&Year)-(?&Month)-(?&Day)|(?&Year)(?&Month)(?&Day))
Engines
| Engine | Supported |
|---|---|
| Perl | ✔ |
| PCRE | ✔ |
| Boost.Regex | ✔ |
| .NET | ❌ |
| Oniguruma | ✔ |
| Hyperscan | ❌ |
| ICU | ❌ |
| Glib/GRegex | ✔ |
| ECMAScript | ❌ |