expression

regex::escape: literal pattern text

Escape metacharacters when a literal text fragment must be included in a pattern. This is not shell or SQL escaping. This example uses the external regex crate; see the staged Cargo manifest. Expected stdout (not executed): true false Common mistake: Interpolating a literal search phrase as arbitrary regex syntax.

Formal Syntax & Structure

rust syntaxrust
regex::escape(text) -> String

Working Example

Example Snippetrust
fn main() {    let literal = "a+b";    let re = regex::Regex::new(&regex::escape(literal)).unwrap();    println!("{} {}", re.is_match("a+b"), re.is_match("aaab"));}
Rust Reference