更改我的正则表达式后,在线程“主” java.lang.NullPointerException中获取异常

时间:2019-07-29 22:29:58

标签: java regex runtime-error runtimeexception

我正在尝试检查文件夹中的文件,以识别与我的正则表达式匹配的文件名。我的正则表达式匹配名称错误的文件。

例如,我将这些文件放在一个文件夹中:

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

正则表达式应该与第一个匹配,因为正确命名的文件名需要使用点而不是空格。

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 个答案:

答案 0 :(得分:0)

我猜测它可能缺少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); 标志,但是不确定。如果是这样,我们的代码可能如下所示:

^(?!(?:(?:(?:\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 1

Java Demo

如果不需要捕获组,我们可以将其更改为非捕获组:

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

RegEx Demo 2

测试

AttributeError: Property '<unknown>.Text' can not be set.