0

I am trying to match list of string that end in .xsd but not in form.xsd I use the following regEx:

ArrayList<String> files = new ArrayList<String>();
files.add("/abadc/asdd/wieur/file1.form.xsd");
files.add("/abadc/asdd/wieur/file2.xsd");

Pattern pattern = Pattern.compile("(?<!form{0,6})\\.xsd$");
for (String file : files) {                                 
    Matcher matcher = pattern.matcher(file);
    if(matcher.find())                                                      
    {                                                                       
        System.out.println("Found >>>> "+file);    
    }                                                                                                                                         
}

I expect file2 to be printed out but i do not get any result. Am i doing something wrong here? I try the same expression in an online java regEx Tester and I get the expected result but I dont get the result in my program.

Rush
  • 35
  • 4

1 Answers1

1

Well, your code example works for me.... but the {0,6} after the 'm' makes no sense..... why can there be 0 to 6 'm's ?

The expression:

"(?<!form)\\.xsd$"

would make more sense, but then I would also change your loop to use the matches() method, and change the regex accordingly:

Pattern pattern = Pattern.compile(".+(?<!form)\\.xsd");
for (String file : files) {                                 
    Matcher matcher = pattern.matcher(file);
    if(matcher.matches())                                                      
    {                                                                       
        System.out.println("Found >>>> "+file);
    }
}
rolfl
  • 17,539
  • 7
  • 42
  • 76
  • Can you please explain me why would I need the .+ while doing a negative lookbehind. I mean I only want to find the pattern that has form.xsd in it right? .+ now says that somethingform.xsd which is not what I want. Since then it will also find abcdform.xsd but i want to find only abcd.form.xsd . Is there any case where my regex would fail. Thanks for the reply. – Rush Oct 08 '13 at 23:11