0

I am trying to check if a directory represented as a String contains as a substring one of the following strings

  \.. OR ..\ OR /.. OR ../ 

however the following code :

 System.out.println("C:\\mytext.zip".matches(".*(\\..|..\\|/..|../).*"));

gives true instead of false,where can be the problem in the regex?

Gordon
  • 85
  • 1
  • 11
  • You should escape all the dots and backslashes. `\\\.\.|\.\.\\|/\.\.|\.\./` string pattern, in Java, it will look like `".*(?:\\\\\\.\\.|\\.\\.\\\\|/\\.\\.|\\.\\./).*"` – Wiktor Stribiżew Jul 12 '18 at 08:49
  • `Path path = Paths.get("...");` and then `path.normalize()` would eliminate `..` so you can check for String equality. Normal security usage is to see that the path does not leave the boxed directory for files. (Also `toAbsolutePath`) – Joop Eggen Jul 12 '18 at 08:55

0 Answers0