写在开始,相信很多人会使用正则校验数据,而且写成了工具类

    但是有相当一部分人,会写String的正则表达式。而不是使用Parttern

    简单做了一个比较代码如下


public static void main(String[] args) {
String ip_formatv4 = "^((((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.){3}((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5])))|()$";
String ip_formatv6 = "^([\\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^::([\\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:):([\\da-fA-F]{1,4}:){0,3}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:){2}:([\\da-fA-F]{1,4}:){0,2}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:){3}:([\\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:){7}[\\da-fA-F]{1,4}$|^:((:[\\da-fA-F]{1,4}){1,6}|:)$|^[\\da-fA-F]{1,4}:((:[\\da-fA-F]{1,4}){1,5}|:)$|^([\\da-fA-F]{1,4}:){2}((:[\\da-fA-F]{1,4}){1,4}|:)$|^([\\da-fA-F]{1,4}:){3}((:[\\da-fA-F]{1,4}){1,3}|:)$|^([\\da-fA-F]{1,4}:){4}((:[\\da-fA-F]{1,4}){1,2}|:)$|^([\\da-fA-F]{1,4}:){5}:([\\da-fA-F]{1,4})?$|^([\\da-fA-F]{1,4}:){6}:$";
String ip_formatv46 = "^([\\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^::([\\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:):([\\da-fA-F]{1,4}:){0,3}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:){2}:([\\da-fA-F]{1,4}:){0,2}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:){3}:([\\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$";
  
String FORMAT = ip_formatv4+"|"+ip_formatv6+"|" +ip_formatv46;
 Pattern pattern = Pattern.compile(FORMAT );
 Long start = System.currentTimeMillis();
 for (int i = 0; i < 100000; i++) {
 patternParam("::1:8:82a8:0:0:9",FORMAT );
 }
 System.out.println("patternParam耗时"+(System.currentTimeMillis()-start));
 Long start2 = System.currentTimeMillis();
 for (int i = 0; i < 100000; i++) {
 patternParam2("::1:8:82a8:0:0:9",pattern);
 }
 System.out.println("patternParam2耗时"+(System.currentTimeMillis()-start2));
}
 public static  boolean patternParam(String str, String format) {
        if (null == str || "".equals(str.trim())) {
            return false;
        }
        if (null == format || "".equals(format.trim())) {
            return false;
        }
        Pattern pattern = Pattern.compile(format);
        if (null != pattern) {
            Matcher m = pattern.matcher(str);
            if (null != m && m.matches()) {
                return true;
            }
        }
        return false;
    }
 public static  boolean patternParam2(String str, Pattern pattern) {
 if (null != pattern) {
            Matcher m = pattern.matcher(str);
            if (null != m && m.matches()) {
                return true;
            }
        }
        return false;
 }

嗯,经比较

patternParam耗时2085ms

patternParam2耗时94ms

可以看出效率 相差挺大的,怎么写就自己考虑吧。


乐享:知识积累,快乐无限。