Java regular expression to match file path

I tried to create a regex to match the file path in java like

C: \ a \ Protection \ GHI \ abc.txt

I tried this ([a-zA-Z]:)?(\\[a-zA-Z0-9_-]+)+\\? like the following code

 import java.util.regex.Pattern; public class RETester { public static void main(String arhs[]){ String regularExpression = "([a-zA-Z]:)?(\\[a-zA-Z0-9_-]+)+\\?"; String path = "D:\\directoryname\\testing\\abc.txt"; Pattern pattern = Pattern.compile(regularExpression); boolean isMatched = Pattern.matches(regularExpression,path); System.out.println(path); System.out.println(pattern.pattern()); System.out.println(isMatched); } } 

However, this always gives me a false result. Help me.

thanks

+7
source share
8 answers

Java also uses backslashes, so you need to strip backslashes twice, once for a Java string and once for a regular expression.

 "([a-zA-Z]:)?(\\\\[a-zA-Z0-9_.-]+)+\\\\?" 

Your regular expression matches the literal string [-zA-Z0-9_-] and the literal '?' in the end. I also added a period to allow 'abc.txt'.

However, consider using a different mechanism to determine the correct file names, since there are different schemes (e.g. unix). java.util.File probably throws an exception if the path is invalid, which may be a good alternative, although I don't like to use exceptions for the control flow ...

+15
source

Use this regex:

 "([a-zA-Z]:)?(\\\\[a-zA-Z0-9._-]+)+\\\\?"; 

I added two modifications: you forgot to add . to match abc.txt file abc.txt and backslash escaping ( \\ ).

+3
source

This does not match, because your regex matches only paths, not files. - Correct: it does not accept a period in the name of your file.

And besides, there is a problem with extinction mentioned by the icon.

+1
source

Simply put, you need to replace . on

 ([a-zA-Z]:)?(\\\\[a-zA-Z0-9_.-]+)+\\\\? 

with \\.

. Designed for any character in a regular expression (Java style), and \. specially designed for. character and we need to avoid backslash

+1
source

Since the path contains folders, and the folder name can contain any character other than

? \ /: "* <>

We can use the regular expression below to match the directory path [it uses all the characters that the folder name can provide)

 [A-Za-z]:[A-Za-z0-9\!\@\#\$\%\^\&\(\)\'\;\{\}\[\]\=\+\-\_\~\`\.\\]+ 
+1
source

There are two reasons why this gives you a lie. Firstly, you need \\\\ instead of \\ because you need to avoid these characters. And the second is that you do not have enough dot character, you can insert it before az as ([a-zA-Z]:)?(\\\\[.a-zA-Z0-9_-]+)+\\\\?

0
source

Here is the correct regular expression for the Windows file system:

Regular expression:

 (?:[a-zA-Z]\:)\\([\w-]+\\)*\w([\w-.])+ 

like a java string

 "(?:[a-zA-Z]\\:)\\\\([\\w-]+\\\\)*\\w([\\w-.])+" 
0
source

If it should correspond only to the path of files lying on the same computer on which your application is running, you can use:

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

So, if you run your application on windows, the code above will catch invalid Windows paths, and if you are running on unix, it will catch invalid unix paths, etc.

0
source

All Articles