2009-09-06 48 views
2

match.matches()返回false。這很奇怪,因爲如果我把這個正則表達式和測試字符串rubular.com,顯示兩個匹配。我究竟做錯了什麼?Java正則表達式不工作 - 爲什麼?

Pattern regex = Pattern.compile("FTW(((?!ODP).)+)ODP"); 
    Matcher match = regex.matcher("ZZZMMMJJJOOFTWZMJZMJODPZZZMMMJJJOOOFTWMZJOMZJOMZJOODPZZZMMMJJJOO"); 

    if (match.matches()) { 
     System.out.println("match found"); 
    } 
    else { 
     System.out.println("match not found"); 
    } 

回答

12

Matcher.matches回報是否整個區域的模式匹配。

請嘗試使用find代替。 (當然在你的例子中,這個工作正常。)

8

Matcher.matches()方法試圖將整個字符串匹配到模式。改變你的模式:

".*FTW(((?!ODP).)+)ODP.*" 
相關問題