java regular expression to match file path

后端 未结 8 1873
自闭症患者
自闭症患者 2020-12-03 18:40

I was trying out to create a regular expression to match file path in java like

C:\\abc\\def\\ghi\\abc.txt

I tried this ([

8条回答
  •  长情又很酷
    2020-12-03 19:11

    If it has to match only the path of files lying on the same machine where your app is running, then you can use:

    try{
        java.nio.file.Paths.get(yourPath);
    }(catch InvalidPathException err){
    }
    

    So if you're running your app on windows the code above will catch invalid windows paths and if you're running on unix, it will catch invalid unix paths, etc.

提交回复
热议问题