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.

4

1 回答 1

1

好吧,您的代码示例对我有用....但是 'm' 后面的 {0,6} 没有意义......为什么会有 0 到 6 个 'm's ?

表达方式:

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

会更有意义,但是我也会更改您的循环以使用 matches() 方法,并相应地更改正则表达式:

Pattern pattern = Pattern.compile(".+(?<!form)\\.xsd");
for (String file : files) {                                 
    Matcher matcher = pattern.matcher(file);
    if(matcher.matches())                                                      
    {                                                                       
        System.out.println("Found >>>> "+file);
    }
}
于 2013-10-08T00:02:42.570 回答