expression

Capture expansion in replacements

Replacement text has its own expansion syntax. Braced names make the capture boundary explicit. The original input is not mutated. This example uses the external regex crate; see the staged Cargo manifest. Expected stdout (not executed): blue/red Common mistake: Assuming dollar signs in a replacement are always literal.

Formal Syntax & Structure

rust syntaxrust
${name} or $1

Working Example

Example Snippetrust
fn main() {    let re = regex::Regex::new(r"(?P<first>[a-z]+):(?P<last>[a-z]+)").unwrap();    println!("{}", re.replace_all("red:blue", "${last}/${first}"));}
Rust Reference