Split String by Backslash with Regex

Hey there :hugs:
I can't understand why I can't split by backslash with that:

let re = Regex::new("([\\/][a-zA-Z0-9]*[.]csv").unwrap();
let myStr = "/myPath/csv\\myFile.csv";
let mat = re.find(myStr).unwrap();
let myStr = &myStr[mat.start() + 1 .. mat.end()];
println!("{}", myStr);

You have two syntax errors:

  • The backslash needs to be escaped double (once for string and once for regex): \\\\
    You can use a raw string to avoid double escaping: r#"([\\/][a-zA-Z0-9]*[.]csv"#
  • The ( is not closed

Fixed: Rust Playground

1 Like

Thats it :star_struck:
Thank you :hugs:

1 Like