Jay*_*eep 2 java regex performance
以下是文字示例:
String id = "A:abc,X:def,F:xyz,A:jkl";
Run Code Online (Sandbox Code Playgroud)
以下是正则表达式:
Pattern p = Pattern.compile("(.*,)?[AC]:[^:]+$");
if(p.matcher(id).matches()) {
System.out.println("Hello world!")
}
Run Code Online (Sandbox Code Playgroud)
执行上面代码时应打印Hello world!.
是否可以修改此正则表达式以获得更高的性能?
由于我看不到你的整个代码,我只能假设你在loop/method/etc中进行模式编译.可以提高性能的一件事是在类级别进行编译,而不是每次都重新编译模式.除此之外,我没有看到你可以改变的其他东西.