Feature: Conditional Expressions (original) (raw)
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.(?(R_n_)…)
— 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 | ❌ |