2013-10-07 12 views
0

我想匹配結束的.xsd但不是在form.xsd我用下面的正則表達式的字符串列表正則表達式負回顧後:在Java中

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);  
    }                                   
} 

我希望文件2被打印出來,但我做的沒有得到任何結果。我在這裏做錯了什麼?我在一個在線的java regEx Tester中嘗試了相同的表達式,我得到了預期的結果,但是我沒有在我的程序中得到結果。

回答

1

那麼,你的代碼示例適用於我......但'm'後面的{0,6}沒有任何意義.....爲什麼可以有0到6'm?

表達:

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

會更有意義,但後來我也想改變你的循環中使用的火柴()方法,並改變相應的正則表達式:

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

可以請你解釋我爲什麼需要+。而做一個消極的後臺。我的意思是我只想找到其中有form.xsd的模式吧? 。+現在說那個somethingform.xsd不是我想要的。從那以後,它也會找到abcdform.xsd,但我只想找到abcd.form.xsd。有沒有我的正則表達式會失敗的情況。謝謝回覆。 – Rush