Regex - replace part of the string

Regex Captures replace_all

Dependencies

examples/regex-replace-text-by-text/Cargo.toml

[package]
name = "regex-replace-text-by-text"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
regex = "1.10.3"

Full code

examples/regex-replace-text-by-text/src/main.rs

use regex::{Captures, Regex};

fn main() {
    // replace every sequnce of letters by x (but not the digits)
    let text = String::from("abc123def");
    let re = Regex::new(r"[A-Za-z]+").unwrap();
    let result = re.replace_all(&text, "x");
    println!("{result}"); // x123x

    // replace every 3 characters by a y
    let text = String::from("abc123def");
    let re = Regex::new(r"...").unwrap();
    let result = re.replace_all(&text, "y");
    println!("{result}"); // yyy

    // Swap every two charcters
    let text = String::from("abc123def");
    let re = Regex::new(r"(.)(.)").unwrap();
    let result = re.replace_all(&text, r#"$2$1"#);
    println!("{result}"); // ba1c32edf

    // duplicate every digit
    let text = String::from("abc123def");
    let re = Regex::new(r"(\d)").unwrap();
    let result = re.replace_all(&text, r#"$1$1"#);
    println!("{result}"); // abc112233def

    // double every digit
    // Replace using a function to generated the replacement
    let text = String::from("abc123def");
    let re = Regex::new(r"(\d)").unwrap();
    let result = re.replace_all(&text, double);
    println!("{result}"); // abc246def

    // double every digit
    // The same inlined
    let text = String::from("abc123def");
    let re = Regex::new(r"(\d)").unwrap();
    let result = re.replace_all(&text, |caps: &Captures| {
        format!("{}", 2 * caps[1].parse::<u8>().unwrap())
    });
    println!("{result}"); // abc246def
}

fn double(caps: &Captures) -> String {
    //String::new()
    //format!("{}", 2 * caps.get(1).unwrap().as_str().parse::<u8>().unwrap())
    format!("{}", 2 * caps[1].parse::<u8>().unwrap())
}

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo