校驗 IP 正則

校驗 IP 正則

String regex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
   @Test
   public void ip() {
       String ip = "192.9.10.255";
       String regex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
       long start = System.nanoTime();
       boolean result = Pattern.matches(regex, ip);
       System.out.println(System.nanoTime() - start);
       start = System.nanoTime();
       boolean iPv4LiteralAddress = IPAddressUtil.isIPv4LiteralAddress(ip);
       System.out.println(System.nanoTime() - start);
       System.out.println(result + "\t" + iPv4LiteralAddress);
   }

執行效率:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章