Quick Start
Tutorial
Tools & Languages
Examples
Reference
Book Reviews
Replacement Reference
Introduction
Table of Contents
Characters
Matched Text & Backreferences
Context & Case Conversion
Conditionals
Regular Expressions Reference
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference
Book Reviews
Printable PDF
About This Site
RSS Feed & Blog
RegexBuddy—Better than a regular expression reference!

Replacement Strings Reference: Conditionals

FeatureSyntaxDescriptionExampleJGsoft .NET Java Perl PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE Oracle XPath
Conditional ?1yes:no through ?99yes:no Conditional referencing a numbered capturing group. Inserts the “yes” part if the group participated or the “no” part if it didn’t. Replacing all matches of (y)?|n in yyn! with ?1yes:no yields yesyesno! nonononononononononononononoallnonono
Conditional (?1yes:no) through (?99yes:no) Conditional referencing a numbered capturing group. Inserts the “yes” part if the group participated or the “no” part if it didn’t. Replacing all matches of (y)?|n in yyn! with (?1yes:no) yields yesyesno! V2nononononononononononononoallnonono
Conditional (?10yes:no) through (?99yes:no) When there are fewer capturing groups than the 2-digit number, treat this as a single-digit conditional with the “yes” part starting with a literal number instead of as an invalid conditional. Replacing all matches of (y)?|n in yyn! with (?19yes:no) yields 9yes9yesno! V2n/an/an/an/an/an/an/an/an/an/an/an/an/anon/an/an/a
Conditional ?{1}yes:no through ?{99}yes:no Conditional referencing a numbered capturing group. Inserts the “yes” part if the group participated or the “no” part if it didn’t. Replacing all matches of (y)?|n in yyn! with ?{1}yes:no yields yesyesno! nonononononononononononononoall
1.42–1.83
nonono
Conditional (?{1}yes:no) through (?{99}yes:no) Conditional referencing a numbered capturing group. Inserts the “yes” part if the group participated or the “no” part if it didn’t. Replacing all matches of (y)?|n in yyn! with (?{1}yes:no) yields yesyesno! V2nononononononononononononoall
1.42–1.83
nonono
Conditional ${1:+yes:no} through ${99:+yes:no} Conditional referencing a numbered capturing group. Inserts the “yes” part if the group participated or the “no” part if it didn’t. Replacing all matches of (y)?|n in yyn! with ${1:+yes:no} yields yesyesno! V2nononoextendednonononononononononononono
Conditional ${1:-no} through ${99:-no} Conditional referencing a numbered capturing group. Inserts the text captured by the group if it participated or the contents of the conditional if it didn’t. Replacing all matches of (y)?|n in yyn! with ${1:-no} yields yyno! V2nononoextendednonononononononononononono
Conditional Any numbered conditional A conditional that references the number of a capturing group that does not exist acts as a conditional to a group that never participates. Replacing all matches of (y)?|n in yyn! with (?9yes:no) yields nonono! V2 errorn/an/an/aextended errorn/an/an/an/an/an/an/an/an/aalln/an/an/a
Conditional $?{name}yes:no Conditional referencing a named capturing group. Inserts the “yes” part if the group participated or the “no” part if it didn’t. Replacing all matches of (?'one'y)?|n in yyn! with ?{one}yes:no yields yesyesno! nonononononononononononononoall
1.42–1.83
nonono
Conditional (?{name}yes:no) Conditional referencing a named capturing group. Inserts the “yes” part if the group participated or the “no” part if it didn’t. Replacing all matches of (?'one'y)?|n in yyn! with (?{one}yes:no) yields yesyesno! V2nononononononononononononoall
1.42–1.83
nonono
Conditional ${name:+yes:no} Conditional referencing a named capturing group. Inserts the “yes” part if the group participated or the “no” part if it didn’t. Replacing all matches of (?'one'y)?|n in yyn! with ${one:+yes:no} yields yesyesno! V2nononoextendednonononononononononononono
Conditional ${name:-no} Conditional referencing a named capturing group. Inserts the text captured by the group if it participated or the contents of the conditional if it didn’t. Replacing all matches of (?'one'y)?|n in yyn! with ${one:-no} yields yyno! V2nononoextendednonononononononononononono
Conditional Any named conditional A conditional that references the name of a capturing group that does not exist is treated as literal text. Replacing all matches of (y)?|n in yn! with (?{name}yes:no) yields ?{name}yes:no?{name}yes:no! V2 errorn/an/an/aextended errorn/an/an/an/an/an/an/an/an/aall
1.42–1.83
n/an/an/a
FeatureSyntaxDescriptionExampleJGsoft .NET Java Perl PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE Oracle XPath