How to use variable in Regex with Rust

1 Answer

0 votes
use regex::Regex;

fn main() {
    let str = String::from("java php go c python php phpphphp php java");
    let word = "php";
    let replacewith = "RUST";

    let re = Regex::new(&format!("(?i){}", word)).unwrap();
    let result = re.replace_all(&str, replacewith);

    println!("{}", result);
}


 
 
/*
run:

java RUST go c python RUST RUSTRUSThp RUST java

*/

 



answered Aug 25, 2024 by avibootz
...