Domanda

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.

È stato utile?

Soluzione

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);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top