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: https://play.rust-lang.org/?gist=1a85904f23b6a195362ba7c5aeb48067&version=stable

1 Like

Thats it :star_struck:
Thank you :hugs:

1 Like