Pattern.quote不按预期工作

问题描述:

我写了一个单元测试来帮助我理解Pattern.quote,对于我的生活无法弄清楚为什么这个测试没有通过。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()); 
} 

字符es字面上,不符合String价值test。但是他们确实出现在String。您可以使用matcher#find()来检查。