// include the latest version of the regex crate in your Cargo.toml
extern crate regex;
use regex::Regex;
fn main() {
let regex = Regex::new(r#"<span class="italics">([^<]*?)</span>"#).unwrap();
let string = "What I am trying to do is to change all instances of <span class=\"italics\">italicised text here</span> to <em>italicised text here</em> where the text in the middle changes throughout the document. I can't just find replace, because there are 598 instances of <span class=\"italics\">, and 700 cases of </span>. Also, I want to learn how to do the find replace with leaving the stuff in the middle alone.
Do I use (.+?) at some point? I've been googling but unable to figure it out. Thank you in advance for your help!
Okay, your string finds the 598 I need to replace in my document, what can I add to the ([\\S\\s]*?) to get it to replace the span tags and leave the text between the span tags alone? For example, if it's <span class=\"italics\">The New York Times</span> and <span class=\"italics\">The Hitchhiker's Guide to the Galaxy</span>, is it possible to change those to <em\">The New York Times</em> and <em>The Hitchhiker's Guide to the Galaxy</em> in one go, or do I have to do them individually?";
let substitution = "<em>\\1</em>";
// result will be a String with the substituted value
let result = regex.replace_all(string, substitution);
println!("{}", result);
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Rust, please visit: https://docs.rs/regex/latest/regex/