Split String by Backslash with Regex


#1

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);

#2

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


#3

Thats it :star_struck:
Thank you :hugs: