2017-05-29 92 views
-3

我正在使用正則表達式從字符串中獲取單詞,它在字母數字的情況下工作正常,但如果使用算術運算符則返回錯誤答案。正則表達式在所有情況下都不能正常工作

Matcher oMatcher; 
Pattern oPattern; 


String key = "a++"; 
oPattern = Pattern.compile("\\b" + key + "\\b"); 
oMatcher = oPattern.matcher("max winzer® build-a-chair cocktailsessel »luisa« in runder form, zum selbstgestalten"); 
if (oMatcher.find()) { 
    System.out.println("True"); 
} 
+2

什麼是你想完全匹配?一個模式中的「++」看起來不正確,但是這些加號沒有逃脫。 –

+0

a ++是一些東西的代碼。 –

回答

3

你要逃避key任何潛在的正則表達式特殊字符Pattern.quote

oPattern = Pattern.compile("\\b" + Pattern.quote(key) + "\\b"); 
            ^^^^^^^^^^^^^ 
+0

謝謝,我完全想要這個。 –

相關問題