Quick Start
Tutorial
Search & Replace
Tools & Languages
Examples
Reference
Replacement Text Tutorial
Introduction
Characters
Non-Printable Characters
Matched Text
Backreferences
Match Context
Case Conversion
Conditionals
Control Verb Argument
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

Backtracking Control Verb Argument

Perl and PCRE2 provide away for the replacement string to insert the argument of the last backtracking control verb that participated in the match.

PCRE2 supports $*MARK and ${*MARK} as part of its replacement text syntax. Replacing a+(*:LEFT)|c+(*:RIGHT) with $*MARK in abc yields LEFTbRIGHT in PCRE2. The curly braces are needed if you want to include some letters and digits immediately after the argument in the replacement string. If the regex does not have any backtracking control verbs with arguments or none of them participated in the match attempt then $*MARK and ${*MARK} are replaced with the empty string.

Perl stores the argument of the last control verb that was involved in the match in the variable $REGMARK. In a Perl script you can use this variable in the replacement because Perl interpolates variables in replacement strings. Replacing a+(*:LEFT)|c+(*:RIGHT) with $REGMARK in abc yields LEFTbRIGHT in a Perl script. If the regex does not have any backtracking control verbs with arguments or none of them participated in the match attempt then $REGMARK is set to TRUE which is interpolated as 1. So replacing [ab] with $REGMARK in abc yields 1b1.