1

I have the following file name:

ABC 14-15 PCEE qwerty checklist - checked by XYZ IDFFCFYYL-01 BB.xlsx

I am using the following regex to test whether the file has XLSX extension:

String filename = "ABC 14-15 PCEE qwerty checklist - checked by XYZ IDFFCFYYL-01 BB.xlsx";    
private static final String checkXLSXfile = "([^\\s]+(\\.(?i)(xlsx))$)";
private static final Pattern pattern = Pattern.compile(checkXLSXfile);
if (pattern.matcher(filename).matches()) {
    System.out.println("Heaven"); 
}
else { 
    System.out.println("HELL"); 
}

However, the pattern fails for this file name. Can anyone help me resolve this?

Byron Wall
  • 3,970
  • 2
  • 13
  • 29
AngryPanda
  • 1,261
  • 2
  • 19
  • 42
  • Please try to replace matches() with find(). See http://stackoverflow.com/questions/4450045/difference-between-matches-and-find-in-java-regex – Javakid Jun 01 '15 at 03:47

2 Answers2

3

Why not just use the String.endsWith(String) method?

if(filename.toLowerCase().endsWith(".xlsx"))
{
 // Do something
}
else
{
  // Do something else
}
Catchwa
  • 5,845
  • 4
  • 31
  • 57
0

this is the test i did in jshell

jshell> Matcher xlsMatcher = xlsPattern.matcher("ABC 14-15 PCEE qwerty checklist - checked by XYZ IDFFCFYYL-01 BB.xlsx");

xlsMatcher ==> java.util.regex.Matcher[pattern=(?i).xlsx$ region=0,69 lastmatch=]

jshell> System.out.println( "find "+ xlsMatcher.find() );

find true

Edgardo Genini
  • 653
  • 5
  • 11