爲什麼String.matches
返回true
當兩個值在[]
括號?爲什麼Java正則表達式「。* [two]。*」匹配「一個」?
System.out.println("[one]".matches("(?i).*" + "[two]" + ".*"));
//Why does it return true? Shouldn't "[]" be treated as value?
System.out.println("one".matches("(?i).*" + "two" + ".*"));//OK - prints false
System.out.println("[one]".equals("[two]"));//OK - prints false
System.out.println("one".equals("two"));//OK - prints false
'[]'表示一個正則表達式的內部的字符類。由於您使用接受正則表達式的'matches()'方法,所以它們被解釋爲。嘗試在它們前面添加\ –