我正在尝试检查文件夹中的文件,以识别与我的正则表达式匹配的文件名。我的正则表达式匹配名称错误的文件。
例如,我将这些文件放在一个文件夹中:
-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);
}
}
答案 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)))).*$
如果不需要捕获组,我们可以将其更改为非捕获组:
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));
}
}
AttributeError: Property '<unknown>.Text' can not be set.