在文件夾中的文件:
myfile_mark_1.mp4
myfile_john_2.mp4
myfile_jake_3.mp4
myfile_tristan_4.mp4
yourfile_mark_1.mp4
如果我要查找前綴包含myfile_,後綴不大於3.mp4的(比如"_3.mp4", "_2.mp4", "_1.mp4")。
因此查找結果應該是:
myfile_mark_1.mp4
myfile_john_2.mp4 and
myfile_jake_3.mp4
不知道怎麼實現?
我的代碼:
String myDirectory = Environment.getExternalStorageDirectory().getAbsolutePath();
File f = new File(myDirectory);
if(f.exists() && f.isDirectory()){
final Pattern p = Pattern.compile("myfile_*\\_(^0*(1?\\d|%d)$).mp4"); // I know I really have a stupid mistake on the regex;
File[] flists = f.listFiles(new FileFilter(){
@Override
public boolean accept(File file) {
return p.matcher(file.getName()).matches();
}
});
String s = "wait a minute, i'm debugging";
}
正則表達式的要求相當簡單:
myfile_.*_[123]\\.mp4