java io - 正则表达式不起作用 - 为什么

时间:2016-10-02 10:02:21

标签: java regex java-io

我写了一个程序来打印扩展名为doc或pdf的文件。但它不起作用。相同的正则表达式使用直接文件名而不是从文件系统检索的文件名,它正常工作。 怎么了:

import static java.nio.file.FileVisitResult.CONTINUE;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class FindDuplicateFiles {

    public static void main(String[] args) throws IOException {

        String startingDirStr ="/Users/ravi/documents/LATEST SOURCE";//"."; 
        String pattern ="([^\\s]+(\\.(?i)(doc|docx|pdf))$)";//"*.doc*";//"((\\.(?i)(pdf|doc))$)";//"^(?:.*\\.(?:docx|pdf)$";// ".*\\.(?:doc|pdf)$";//

        Path startingDir = Paths.get(startingDirStr);

        Finder finder = new Finder(pattern);
        Files.walkFileTree(startingDir, finder);
        finder.done();
    }

    public static class Finder extends SimpleFileVisitor<Path> {

        private final PathMatcher matcher;
        private int numMatches = 0;

        Finder(String pattern) {
            matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
        }

        // Compares the glob pattern against
        // the file or directory name.
        void find(Path file) {
            Path name = file.getFileName();
            if (name != null && matcher.matches(name)) {
                numMatches++;

                System.out.println(name);
                //System.out.println(file);
            }
        }

        // Prints the total number of
        // matches to standard out.
        void done() {
            System.out.println("Matched: " + numMatches);
        }

        // Invoke the pattern matching
        // method on each file.
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            find(file);
            return CONTINUE;
        }

        // Invoke the pattern matching
        // method on each directory.
        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
            find(dir);
            return CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) {
            System.err.println(exc);
            return CONTINUE;
        }
    }

}

但它运作良好:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FindDocuments {

    private Pattern pattern;
    private Matcher matcher;

    private static final String IMAGE_PATTERN = "([^\\s]+(\\.(?i)(doc|docx|pdf))$)";

    public FindDocuments() {
        pattern = Pattern.compile(IMAGE_PATTERN);
    }

    public boolean validate(final String image) {

        matcher = pattern.matcher(image);
        return matcher.matches();

    }

    public static void main(String[] args) {
        FindDocuments fd = new FindDocuments();
        String[] aaa = new String[] { "a.doc", "a.docx", "a.pdf", "a.PDF", "..DOC", "..PDF" };
        for (String temp : aaa) {
            boolean valid = fd.validate(temp);
            System.out.println("Image is valid : " + temp + " , " + valid);

        }

    }
}

1 个答案:

答案 0 :(得分:1)

你告诉FileSystem.getPathMatcher(String)将模式解释为“glob”而不是“regex”。