0

I am trying to filter files using FilenameFilter to grep files files in a directory.

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

I am trying to filter node.l and rels.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

my regex : (?s)node.l.?(?=(rels.l))

but when I run it through DirList.java filter it doesn't work..

% 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

Any ideas?

4

2 回答 2

1

DirList正在分别针对每个文件名评估您的正则表达式,而不是作为 . 返回的单个\n分隔目录列表字符串ls。在这些条件下,您的正则表达式永远不会匹配,因为它一次不会看到多个文件名。

于 2013-08-27T22:26:55.507 回答
1

FilenameFilter 适用于单个文件而不是文件组,因此正则表达式将仅应用于该单个文件。而不是正则表达式尝试这样的方式:

  1. 取文件名
    • 如果是node.l检查是否new File(currentDir+"/rels.l").exists()
    • 如果是rels.l检查是否new File(currentDir+"/node.l").exists()
于 2013-08-27T22:29:21.937 回答