// 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"(?xm)[^[:graph:]*](?#Do not match on any visible character)
\K\*(?#Reset starting point and then match on a *)
([[:alpha:].,\ ]+(?:\*{2}[[:alpha:].,\ ]+\*{2}?\ ?)?)(*COMMIT)
\*\ ?(?#Match on ending * then stop matching with COMMIT)").unwrap();
let string = "This text is not italic.
*This text is italic.*
This text is *partially* italic
This text has *two* *italic* bits
**bold text (not italic)**
**bold text with *italic* **
**part bold,** *part italic*
*italic text **with bold** *
*italic* **bold** *italic* **bold**
*invalid markdown (do not parse)**
random * asterisk";
let substitution = "<i>$1</i> ";
// 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/