简体   繁体   English

java FilenameFilter正则表达式(展望未来)

[英]java FilenameFilter regex (look ahead)

I am trying to filter files using FilenameFilter to grep files files in a directory. 我正在尝试使用FilenameFilter过滤文件以grep目录中的文件文件。

% ls -1
DirFilter.class
DirList.class
DirList.java
doctors.txt
node.l
rels.l

I am trying to filter node.l and rels.l . 我试图过滤node.lrels.l Filter should succeed if and only if both files are present. 当且仅当两个文件都存在时,过滤器才会成功。

I tried my regex on debuggex.com and it seems to work as expected : http://www.debuggex.com/embed/CZgVeUE2iWsNfRNG 我在debuggex.com上尝试了我的正则表达式,它似乎按预期工作: http//www.debuggex.com/embed/CZgVeUE2iWsNfRNG

my regex : (?s)node.l.?(?=(rels.l)) 我的正则表达式:( (?s)node.l.?(?=(rels.l))

but when I run it through DirList.java filter it doesn't work.. 但是当我通过DirList.java过滤器运行它时它不起作用..

% java DirList "(?s)node.l.?(?=(rels.l))"
<no-output>

Now I am using DirList.java from Thinking in Java http://www.cs.odu.edu/~cs476/tijava2/c10/DirList.java 现在我使用Thinking in Java中的DirList.java http://www.cs.odu.edu/~cs476/tijava2/c10/DirList.java

Any ideas? 有任何想法吗?

DirList is evaluating your regex against each file name separately, not as a single \\n delimited directory listing string as returned by ls . DirList分别针对每个文件名评估您的正则表达式,而不是ls返回的单个\\n分隔目录列表字符串。 Your regex will never match under those conditions since it never sees more than one file name at a time. 你的正则表达式永远不会在这些条件下匹配,因为它一次看不到多个文件名。

FilenameFilter works for single file not for groups of files so regex will be applied only to that single file. FilenameFilter适用于单个文件而不适用于文件组,因此正则表达式仅适用于该单个文件。 Instead of regex try maybe this way: 而不是正则表达式尝试可能这样:

  1. take name of file 取名字的文件
    • if it is node.l check if new File(currentDir+"/rels.l").exists() . 如果是node.l请检查是否有new File(currentDir+"/rels.l").exists()
    • if it is rels.l check if new File(currentDir+"/node.l").exists() . 如果它是rels.l检查是否有new File(currentDir+"/node.l").exists()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM