java文件名过滤模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3057621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:03:47  来源:igfitidea点击:

java filenames filter pattern

javaregexfilterfilenames

提问by Serg

I need to implement

我需要实施

File[] files = getFiles( String folderName, String ptrn );

Where ptrn is a command prompt style pattern like "*2010*.txt"

其中 ptrn 是命令提示符样式模式,如“*2010*.txt”

I'm familar with FilenameFilter class, but can't implement public boolean accept(File dir, String filename)because String.matches() doesn't accept such patterns.

我熟悉 FilenameFilter 类,但无法实现, public boolean accept(File dir, String filename)因为 String.matches() 不接受此类模式。

Thanks!

谢谢!

采纳答案by BalusC

The String#matches()accepts regular expressionpatterns.

所述String#matches()接受的正则表达式模式

The regex variant of the "layman's" variant *2010*.txtwould be .*2010.*\.txt.

“外行”变体的正则表达式变体*2010*.txt将是.*2010.*\.txt.

So the following should work:

所以以下应该工作:

public boolean accept(File dir, String name) {
    return name.matches(".*2010.*\.txt");
}

The double backslash is just there to represent an actual backslash because the backslash itself is an escape character in Java's String.

双反斜杠仅用于表示实际的反斜杠,因为反斜杠本身是 Java 的String.

Alternatively, you can also do it without regex using the other Stringmethods:

或者,您也可以使用其他String方法在没有正则表达式的情况下执行此操作:

public boolean accept(File dir, String name) {
    return name.contains("2010") && name.endsWith(".txt");
}

Your best bet is likely to let ptrnrepresent a realregex pattern or to string-replace every .with \.and *with .*so that it becomes a valid regex pattern.

您最好的选择可能是 letptrn代表一个真正的正则表达式模式,或者将每个.with\.*with字符串替换.*,使其成为有效的正则表达式模式。

public boolean accept(File dir, String name) {
    return name.matches(ptrn.replace(".", "\.").replace("*", ".*"));
}

回答by OscarRyz

You may need to scape your specific wild cards for those used in Java regex.

您可能需要为 Java 正则表达式中使用的特定通配符转义。

For instance to replace "*" you could use something like:

例如,要替换“*”,您可以使用以下内容:

import java.io.*;

class Filter {
    public static void main ( String [] args ) {
        String argPattern = args[0];

        final String pattern = argPattern.replace(".","\.").replace("*",".*");
        System.out.println("transformed pattern = " + pattern );
        for( File f : new File(".").listFiles( new FilenameFilter(){
                           public boolean accept( File dir, String name ) { 
                               return name.matches( pattern );
                           }
                        })){
             System.out.println( f.getName() );
        }
    }
}


$ls -l *ter.*
-rw-r--r--  1 oscarreyes  staff  1083 Jun 16 17:55 Filter.class
-rw-r--r--  1 oscarreyes  staff   616 Jun 16 17:56 Filter.java
$java Filter "*ter.*"
transformed pattern = .*ter\..*
Filter.class
Filter.java