Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glob pattern using getPathMatcher

Tags:

java

path

glob

From the book of "Kathy Sierra Bert Bates" for OCP exam I found the following code

public class FileTest {

    public static void matches(Path path, String glob){
        PathMatcher matcher = FileSystems.getDefault().getPathMatcher(glob);
        System.out.println(matcher.matches(path));
    }
    public static void main(String[] args) throws IOException {
        Path path = Paths.get("/com/java/One.java");
        matches(path, "glob:*.java");
        matches(path, "glob:**/*.java");
        matches(path, "glob:*");
        matches(path, "glob:**");

    }
}

Output:

false
true
false
true

I can not understand the output clearly. Would any one explain me. let me know my example what is crossing directory boundaries. Thanks Rocky

like image 426
Salahin Rocky Avatar asked Jan 03 '15 04:01

Salahin Rocky


2 Answers

matches(path, "glob:*.java"); // => flase

because your path contains / which describe a directory hierarchy, *.java matches any file name with .java extension

matches(path, "glob:**/*.java"); // => true

because ** matches any string, including sub-path (like /com/java/ in you example)

matches(path, "glob:*"); // => false

as mentioned in the first one, because you have path separator /

matches(path, "glob:**"); // => true

as mentioned in the second one, because ** matches any string including /

like image 185
Adil Avatar answered Oct 23 '22 16:10

Adil


public class FileTest {

    public static void matches(Path path, String glob){
        PathMatcher matcher = FileSystems.getDefault().getPathMatcher(glob);
        System.out.println(matcher.matches(path));
    }
    public static void main(String[] args) throws IOException {
        Path path = Paths.get("/com/java/One.java");
        matches(path, "glob:*.java");       // regular expression that matches any file path that end with .java so it will return the value as true
        matches(path, "glob:**/*.java"); // regular expression ** characters matches zero or more characters crossing directory boundaries so it will match complete path but if you put /* it will search for a path like this /com/java//one.java soe here it will not match the path and will return value as false.
        matches(path, "glob:*"); // this will match any path and return value as true.
        matches(path, "glob:**"); // this will complete path crossing directory so it will return you value as true.

    }
}

In the above program when you are calling matches with the path as "/com/java/One.java" and glob regular expression for searching or matching the path the function will take the values and perform the operation and return true or false. Output:

true
false
true
true

If you are using the windows platform then you need to modify your program as follows.

public class match {

  public static void matches(Path path, String glob){
    PathMatcher matcher = FileSystems.getDefault().getPathMatcher(glob);
    System.out.println(matcher.matches(path));
  }
  public static void main(String[] args) throws IOException {
    Path path = Paths.get("\\com\\java\\One.java");
    matches(path, "glob:*.java");
    matches(path, "glob:**\\*.java");
    matches(path, "glob:*");
    matches(path, "glob:**");

  }
}

For more details Click here

like image 23
smali Avatar answered Oct 23 '22 15:10

smali