【转载】Jave开发手册之正则表达式预编译

 

 今天又水一篇,java开发手册华山版

一、编程规约 (九)其它 第一条

解释: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()) {
        ...
    }
}

摘自:https://blog.csdn.net/qq_35312171/article/details/82663344

 

 

 

 

 

  

相关文章:

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