0

I enter from the console the path to the directory in which I want to search for certain files. I need a regex that only matches directory paths, not file paths. For example,

"C:\directory", "C:\directory\another", "D:\directory\", "D:\" must match.

"C:\directory\file.txt", "D:\another.pdf" should not match.

abdo Salm
  • 1,678
  • 4
  • 12
  • 22
  • [The regex seem to match files in subpaths as well](https://regex101.com/r/rt4Vxv/1). But it seems also to match other paths, e.g. paths ending in a `\ `. For me, the question is unclear. Please [edit] the post and specify what exactly the requirements are. – Turing85 Sep 24 '22 at 10:09
  • 1
    Please be aware that dots in folder names are perfectly valid in Windows. There's nothing illegal about having a folder called `c:\my.txt` – Dawood ibn Kareem Sep 24 '22 at 10:11
  • [This post](https://stackoverflow.com/questions/4736/learning-regular-expressions) as well as [`regular-expression.info`](https://www.regular-expressions.info/reference.html) might be valuable resources. – Turing85 Sep 24 '22 at 10:12
  • 1
    ```File.isDirectory``` seems to be what you're reaching for ... (which would solve the problem that @DawoodibnKareem rightly brings up) – g00se Sep 24 '22 at 10:14
  • Thanks everybody. Especially, @g00se. Your solution is just what I've been looking for! – PrettyCoder Sep 24 '22 at 10:20
  • 1
    :) Or ```java.nio.file.Files.isDirectory``` – g00se Sep 24 '22 at 10:30
  • 1
    Nothing stops you from creating a directory called `C:\directory\file.txt`, similarly, `C:\directory\another` is also valid as a file name, not just a directory. Don't apply arbitrary rules that don't exist for the underlying problem. – Mark Rotteveel Sep 24 '22 at 15:06

0 Answers0