Question

Suppose I have files like these in a folder:

  • myfile_mark_1.mp4
  • myfile_john_2.mp4
  • myfile_jake_3.mp4
  • myfile_tristan_4.mp4
  • yourfile_mark_1.mp4

How do I find files where only the names containing "myfile_" in the prefix and suffix would be 3 or less("_3.mp4", "_2.mp4", "_1.mp4", something like this).

So in my program I could get:

  • myfile_mark_1.mp4
  • myfile_john_2.mp4 and
  • myfile_jake_3.mp4

I have tried this one, hoping to get lucky but I didn't get any result. :D

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";
}

I hope someone can help out on my problem.

Was it helpful?

Solution

The regex required is quite simple.

myfile_.*_[123]\\.mp4

OTHER TIPS

try this :-

 String myDirectory = Environment
        .getExternalStorageDirectory().getAbsolutePath();
 File f = new File(myDirectory);
 if (f.exists() && f.isDirectory()) {
    final Pattern p = Pattern.compile("yourfile_.*_[123]\\.png"); 
 File[] flists = f.listFiles(new FileFilter() {
    @Override
    public boolean accept(File file) {
        p.matcher(file.getName()).matches();
        Log.e("MIS", "sdsdsdsds" + p.matcher(file.getName()).matches());

        return p.matcher(file.getName()).matches();

    }
});
String s = "wait a minute, i'm debugging";

}

If you want to do this with a Pattern, here's a working example, since the Pattern you use has many issues.

String mark = "myfile_mark_1.mp4";
String john = "myfile_john_2.mp4";
String jake = "myfile_jake_3.mp4";

Pattern pattern = Pattern.compile("myfile_\\p{Alpha}+_\\d+\\.mp4");
Matcher matcher = pattern.matcher(mark);
System.out.println(matcher.find());
matcher = pattern.matcher(john);
System.out.println(matcher.find());
matcher = pattern.matcher(jake);
System.out.println(matcher.find());

Output:

true
true
true

Issues I found in the Pattern you use:

  • I interpreted the ^0 part as an optional starting character. The ^ character can mean the beginning of the whole input, or the negation of a character or group (which only works in character classes).
  • The . in your Pattern must be escaped.
  • Not sure what the %d means in your Pattern, as it doesn't reflect anything in your examples. Is it because you want to group the numbers and back-reference them? In that case, you can wrap your numerical expression around round brackets - it'll be your index-1 group in this case.

Try some thing like this. I think it is easy

 String fileName="myfile_mark_1.mp4";
    if(fileName.startsWith("myfile")){
       if(Integer.parseInt(fileName.split("_")[2].substring(0,1))<=3){
           System.out.println(fileName);
       }
    }

Following will return all files in your computer match with your criteria.

 public static void main(String[] args) {
    File[] paths = File.listRoots();
    for(File directory:paths){
        getFile(directory.toString());
    }
}

public static void getFile(String directoryName) {
    File directory = new File(directoryName);
    File[] fList = directory.listFiles();
    if(fList!=null){
        for (File file : fList) {
            if (file.isFile()) {
                if(file.getName().toString().startsWith("myfile")){
                    if(Integer.parseInt(file.getName().toString().split("_")[2].substring(0,1))<=3){
                        System.out.println(file.getName().toString());
                    }
                }
            } else if (file.isDirectory()) {
                getFile(file.getAbsolutePath());
            }
        }
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top