亵渎词正则表达式不匹配shi +不正确匹配

时间:2019-01-04 06:25:38

标签: java regex word-boundary

String stre = "da shi+ jia";
profaneWord= "shi+";
boolean flag = isContain(stre, profaneWord);
private static boolean isContain(String source, String subItem) {
    String pattern = subItem;
    String pat = Pattern.quote(pattern);
    System.out.println("pattern quote example  " + pat);
    Pattern p = Pattern.compile("\b"+pat+"\b");
    System.out.println("pattern quote example  " + p.toString());
    Matcher m = p.matcher(source);
    return m.find();
}
  

模式引用示例\ Qshi + \ E模式引用示例\ Qshi + \ E   错误

期望:是

我正在使用Java 7。

1 个答案:

答案 0 :(得分:0)

单词边界转义为\\b而不是\b

Pattern.compile("\\b"+pat+"\\b");