regex_replace!() { /* proc-macro */ }
Expand description

Replaces the leftmost match in the second argument with the value returned by the closure given as third argument.

The closure is given one or more &str, the first one for the whole match and the following ones for the groups. Any optional group with no value is replaced with "".

Example:

let text = "Fuu fuuu";
let text = regex_replace!(
    "f(u*)"i,
    text,
    |_, suffix: &str| format!("F{}", suffix.len()),
);
assert_eq!(text, "F2 fuuu");