我写了一个单元测试来帮助我理解Pattern.quote并且对于我的生活无法弄清楚为什么这个测试没有通过。
@Test
public void testRegexEscape() throws Exception {
String text = "Test";
String patternString = "es";
String quoted = Pattern.quote(patternString);
Pattern pattern = Pattern.compile(quoted);
Matcher matcher = pattern.matcher(text);
assertTrue(matcher.matches());
}
答案 0 :(得分:4)
字符es
,字面与String
值test
不匹配。然而,它们出现在String
中。您可以使用matcher#find()
来检查。