Escape speech marks in regex


#1

How do you escape speech marks " in a regular expression created with regex? The regular expression I’m using is ID is "([0-9a-zA-Z]{4})". I’ve tried using `’ as an escape character, but results in the following compiler error:

src/main.rs:19:51: 19:52 error: unknown start of token: \\
src/main.rs:19     let re = Regex::new(r"ID is \"([0-9a-zA-Z]{4})\"").unwrap();


#2

You are using a raw string. Thus all characters but the closing " (quote) are ignored, even the backslash.

Raw strings have one trick up their sleeve for this: you can prepend hashs (#) and add the same number after the closing quote:

r####"ID is "([0-9a-zA-Z]{4})""####

#3

Thanks. I misunderstood the meaning of the r in r"...". I thought that meant regular expression :slightly_smiling: Using a normal string I can get this working now, but thanks for the # prepending, that’s good to know.


#4

I am also having a problem with this… I am trying to use:

(r"['"]\\w+['"]: "")

as a regex and having trouble:

error: unknown start of token: \
  --> src/main.rs:93:21
   |
93 |    terms.push(r"['"]\w+['"]: "");
   |                     ^

error: aborting due to previous error

I’m not understanding the limitations of r"" and what needs to be escaped… I’ve tried escaping the \w+ with \w+ and it still won’t work. Also, the regex has " in it literally so could that be an issue as well?


#5

Rust by example seems to have a clear summary of raw strings:

https://doc.rust-lang.org/rust-by-example/std/str.html?highlight=raw,string#literals-and-escapes

Sometimes there are just too many characters that need to be escaped or it’s just much more convenient to write a string out as-is. This is where raw string literals come into play

fn main() {
    let raw_str = r"Escapes don't work here: \x3F \u{211D}";
    println!("{}", raw_str);

    // If you need quotes in a raw string, add a pair of #s
    let quotes = r#"And then I said: "There is no escape!""#;
    println!("{}", quotes);

    // If you need "# in your string, just use more #s in the delimiter.
    // There is no limit for the number of #s you can use.
    let longer_delimiter = r###"A string with "# in it. And even "##!"###;
    println!("{}", longer_delimiter);
}

With a raw string you cannot escape anything with slashes. If you need a " inside your raw string, start it with r#" and end it with "#