Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java regular expression to match file path

Tags:

java

regex

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 ([a-zA-Z]:)?(\\[a-zA-Z0-9_-]+)+\\? , like 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 it's always giving me , false as result .

like image 357
Jijoy Avatar asked Dec 20 '10 12:12

Jijoy


People also ask

What is regex path?

Your regex (\/|. *\/) uses an alternation which matches either a forward slash or any characters 0+ times greedy followed by matching a forward slash. So in for example /ghi/jkl , the first match will be the first forward slash.

What does \\ mean in Java regex?

The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash. If you want to define \w , then you must be using \\w in your regex.

How do you multiply in regular expressions?

The RegExp/\d+\(\d+\)/ will match the a typical multiplication case.


3 Answers

Java is using backslash-escaping too, you know, so you need to escape your backslashes twice, once for the Java string, and once for the regexp.

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

Your regexp matched a literal '[-zA-Z0-9_-' string, and a literal '?' at the end. I also added a period in there to allow 'abc.txt'..

That said, consider using another mechanism for determine valid file names, as there are different schemes (i.e. unix). java.util.File will probably throw an exception if the path is invalid, which might be a good alternative, although I don't like using exceptions for control flow...

like image 182
falstro Avatar answered Oct 16 '22 23:10

falstro


Use this regex:

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

I added two modifications: you forgot to add . for matching the file name abc.txt and backslash escaping (\\) was also needed.

like image 27
darioo Avatar answered Oct 16 '22 23:10

darioo


It does not match, because your regex match only to paths, not to files. -- More correct: it does not accept the dot in your file name.

And in addition, there is the escaping problem mentiond by roe.

like image 1
Ralph Avatar answered Oct 16 '22 22:10

Ralph