Feature: Conditional Expressions
Home | Features | Improve this section
A Conditional Expression checks a condition and evaluates its first alternative if the condition is true; otherwise, it evaluates its second alternative.
Syntax
NOTE: The following syntax is an example based on some of the supported engines. For specific engine support, see Engines.
(?(condition)yes-pattern|no-pattern)— Matches yes-pattern if condition is true; otherwise, matches no-pattern.(?(condition)yes-pattern)— Matches yes-pattern if condition is true; otherwise, matches the empty string.
Conditions
The following conditions are supported:
(?(?=test-pattern)…)— Evaluates to true if a lookahead for test-pattern matches; Otherwise, evaluates to false.(?(?!test-pattern)…)— Evaluates to true if a negative lookahead for test-pattern matches; Otherwise, evaluates to false.(?(n)…)— Evaluates to true if the capture group at offset n was successfully matched; Otherwise, evaluates to false.(?(<name>)…)— Evaluates to true if the named capture group with the name name was successfully matched; Otherwise, evaluates to false.(?('name')…)— Evaluates to true if the named capture group with the name name was successfully matched; Otherwise, evaluates to false.(?(R)…)— Evaluates to true if inside a recursive expression; Otherwise, evaluates to false.(?(Rn)…)— Evaluates to true if inside a recursive expression for the capture group at offset n; Otherwise, evaluates to false.(?(R&name)…)— Evaluates to true if inside a recursive expression for the named capture group with the name name; Otherwise, evaluates to false.(?(DEFINE)…)— Always evaluates to false. This allows you to define Subroutines.
Engines
| Engine | Supported |
|---|---|
| Perl | ✔ |
| PCRE | ✔ |
| Boost.Regex | ✔ |
| .NET | ✔ |
| Oniguruma | ✔ |
| Hyperscan | ❌ |
| ICU | ❌ |
| Glib/GRegex | ✔ |
| ECMAScript | ❌ |