正则表达式越长越复杂,性能越差。对于这样的事情,将输入与三个正则表达式进行匹配应该会更好并且更容易理解。
String match = "quxfoobar";
Pattern start = Pattern.compile("^foo");
Pattern contain = Pattern.compile("\\/foo");
Pattern end = Pattern.compile("bar$");
boolean m = (
!start.matcher(match).find() &&
!contain.matcher(match).find() &&
end.matcher(match).find()
);
编辑:由于在这种情况下三个正则表达式是否会更快存在一些问题,我写了一个基准。当我运行它时,单个正则表达式(取自另一个答案)的速度是使用三个单独的正则表达式运行的三倍。
import java.util.regex.*;
import java.util.*;
public class Test {
private static final Pattern START = Pattern.compile("^foo");
private static final Pattern CONTAIN = Pattern.compile("\\/foo");
private static final Pattern END = Pattern.compile("bar$");
private static final Pattern ONEPATTERN = Pattern.compile("^(?!foo)(\\/(?!foo)|[^\\/])*bar$");
public static void main(String[] args){
String[] in = createInput();
timeOnePattern(in);
timeThreePatterns(in);
System.exit(0);
}
public static String[] createInput(){
String[] words = {"foo","bar","baz","biz","/foo"};
Random rand = new Random();
String[] in = new String[10000];
for (int i=0; i<in.length; i++){
StringBuilder sb = new StringBuilder();
for (int j=0; j<4; j++){
sb.append(words[rand.nextInt(words.length)]);
}
in[i] = sb.toString();
}
return in;
}
public static void timeThreePatterns(String[] in){
long startTime = System.nanoTime();
for (String s: in){
boolean b = (!START.matcher(s).find() && !CONTAIN.matcher(s).find() && END.matcher(s).find());
}
long endTime = System.nanoTime();
System.out.println("Three regular expressionv took " + (endTime - startTime) + " nanoseconds.");
}
public static void timeOnePattern(String[] in){
long startTime = System.nanoTime();
for (String s: in){
ONEPATTERN.matcher(s).matches();
}
long endTime = System.nanoTime();
System.out.println("Single regular expression took " + (endTime - startTime) + " nanoseconds.");
}
}