使用正则表达式的预编译功能,可以有效加快正则匹配速度。
Pattern要定义为static final静态变量,以避免执行多次预编译。
示例:
【错误用法】

// 没有使用预编译
private void func(...) {
    if (Pattern.matches(regexRule, content)) {
        ...
    }
}
// 多次预编译
private void func(...) {
    Pattern pattern = Pattern.compile(regexRule);
    Matcher m = pattern.matcher(content);
    if (m.matches()) {
        ...
    }
}

【正确用法】

private static final Pattern pattern = Pattern.compile(regexRule);
 
private void func(...) {
    Matcher m = pattern.matcher(content);
    if (m.matches()) {
        ...
    }
}

 

相关文章:

  • 2021-11-23
  • 2022-12-23
  • 2021-10-19
  • 2022-12-23
  • 2021-11-05
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-30
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2022-02-22
  • 2021-10-06
  • 2021-07-07
相关资源
相似解决方案