0

I'm trying to check files within a folder to identify the file names that match my regex. My regex matches to incorrectly named files.

So, for example, I have these files in a folder:

-95F Front Anger BW.jpg    
-95F.Front.Anger.C.Micro.jpg    
-95F.Front.Fear.C.Micro.jpg    
-95F.Front.Frown.BW.jpg

The regex should match the first one because correctly named filenames need to have dots instead of whitespaces.

public static void main(String[] args) {
   Pattern patternList = Pattern.compile("^(?!(?:((\\d{1,3}([FM])\\."
             + "(Front|Profile|Right)\\.(Anger|Fear|Frown|Smile)\\."
             + "(BW\\.Micro|BW|C\\.Micro|C)))|(\\d{1,3}(F|M)\\."
             + "(Front|Profile|Right)\\.(Neutral|Smile)\\.(C\\.Micro|C|BW\\"
             + ".Micro|BW|HighLight|LowLight|MedLight)\\.(BW\\.Micro|BW|C\\"
             + ".Micro|C))|(\\d{1,3}(F|M)\\.(Selfie1|Selfie2|StudentID)\\."
             + "(C\\.Micro|C|BW\\.Micro|BW)))).*$"); 

   Scanner in = new Scanner(System.in);
  System.out.print("Enter base directory: ");
  String dir = in.nextLine();

  checkFolder(new File(dir), patternList); //EXCEPTION HERE
 }

private static void checkFolder(File root, Pattern patternList) {
  for(File file : root.listFiles()) //EXCEPTION here

       if(file.isFile()){

           if(!patternList.matcher(file.getName()).matches())

              checkFolder(file, patternList); //EXCEPTION here

           else 
               System.out.println(file); 
       }       
 }
  • 1
    Add full stack trace of the exception please. – LMC Jul 29 '19 at 22:31
  • I've parsed your regex string, don't see any problems there. It's funny to see the catenation `+` before the string segments. I usually but them after. –  Jul 29 '19 at 22:46
  • I think the problem is in the logic of your `checkFolder` method. The recursive call occurs when the `root` parameter is a file (instead of a directory). Also, about your pattern: copy/paste it into an editor in a new document, put spaces between tokens and indent the pattern each time you have parenthesis, remove useless groups and put all you can in factor, turn capture groups into non capturing groups. Instead of writting a negative pattern and to use it in a negated condition, why you didn't write a "positive" pattern with a "positive" condition in `checkFolder`? – Casimir et Hippolyte Jul 29 '19 at 23:31

1 Answers1

0

I'm guessing that it might be missing the m flag, not so sure though. If so, our code might look like:

Pattern patternList = Pattern.compile("^(?!(?:((\\d{1,3}([FM])\\."
             + "(Front|Profile|Right)\\.(Anger|Fear|Frown|Smile)\\."
             + "(BW\\.Micro|BW|C\\.Micro|C)))|(\\d{1,3}(F|M)\\."
             + "(Front|Profile|Right)\\.(Neutral|Smile)\\.(C\\.Micro|C|BW\\"
             + ".Micro|BW|HighLight|LowLight|MedLight)\\.(BW\\.Micro|BW|C\\"
             + ".Micro|C))|(\\d{1,3}(F|M)\\.(Selfie1|Selfie2|StudentID)\\."
             + "(C\\.Micro|C|BW\\.Micro|BW)))).*$", Pattern.MULTILINE);

RegEx Demo 1

Java Demo

If the capturing groups would be unnecessary, we can change those to non-capturing groups:

^(?!(?:(?:(?:\d{1,3}(?:[FM])\.(?:Front|Profile|Right)\.(?:Anger|Fear|Frown|Smile)\.(?:BW\.Micro|BW|C\.Micro|C)))|(?:\d{1,3}(?:F|M)\.(?:Front|Profile|Right)\.(?:Neutral|Smile)\.(?:C\.Micro|C|BW\.Micro|BW|HighLight|LowLight|MedLight)\.(?:BW\.Micro|BW|C\.Micro|C))|(?:\d{1,3}(?:F|M)\.(?:Selfie1|Selfie2|StudentID)\.(?:C\.Micro|C|BW\.Micro|BW)))).*$

RegEx Demo 2

Test

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "^(?!(?:(?:(?:\\d{1,3}(?:[FM])\\.(?:Front|Profile|Right)\\.(?:Anger|Fear|Frown|Smile)\\.(?:BW\\.Micro|BW|C\\.Micro|C)))|(?:\\d{1,3}(?:F|M)\\.(?:Front|Profile|Right)\\.(?:Neutral|Smile)\\.(?:C\\.Micro|C|BW\\.Micro|BW|HighLight|LowLight|MedLight)\\.(?:BW\\.Micro|BW|C\\.Micro|C))|(?:\\d{1,3}(?:F|M)\\.(?:Selfie1|Selfie2|StudentID)\\.(?:C\\.Micro|C|BW\\.Micro|BW)))).*$";
final String string = "95F    Front   Anger   BW\n"
     + "95F Front Anger BW\n"
     + "95F Front Anger.BW\n"
     + "95F.Front.Anger.C.Micro\n"
     + "95F.Front.Fear.C.Micro\n"
     + "95F.Front.Frown.BW\n"
     + "-95F Front Anger BW.jpg    \n"
     + "-95F.Front.Anger.C.Micro.jpg    \n"
     + "-95F.Front.Fear.C.Micro.jpg    \n"
     + "-95F.Front.Frown.BW.jpg";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (int i = 1; i <= matcher.groupCount(); i++) {
        System.out.println("Group " + i + ": " + matcher.group(i));
    }
}
Emma
  • 27,428
  • 11
  • 44
  • 69