java FilenameFilter regex (look ahead)

178 views Asked by At

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?

2

There are 2 answers

0
Jim Garrison On

DirList is evaluating your regex against each file name separately, not as a single \n delimited directory listing string as returned by ls. Your regex will never match under those conditions since it never sees more than one file name at a time.

0
Pshemo On

FilenameFilter works for single file not for groups of files so regex will be applied only to that single file. 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().
    • if it is rels.l check if new File(currentDir+"/node.l").exists().