簡體   English   中英

java io - 正則表達式不起作用 - 為什么

[英]java io - regular expressions not working - Why

我編寫了一個程序來打印擴展名為 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);

        }

    }
}

您告訴FileSystem.getPathMatcher(String)將模式解釋為“glob”而不是“regex”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM