Chinaunix首页 | 论坛 | 博客
  • 博客访问: 364331
  • 博文数量: 102
  • 博客积分: 2000
  • 博客等级: 大尉
  • 技术积分: 1116
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-29 16:21
文章分类

全部博文(102)

文章存档

2014年(10)

2011年(1)

2008年(2)

2007年(89)

我的朋友

分类: 系统运维

2007-04-03 21:23:05

public class Hello {
  public boolean isIPv6(String address)
     {
         boolean result = false;
         String regHex = "(})";
        
         //没有双冒号
         String regIPv6Full = "^(" + regHex + ":){7}" + regHex + "$";
        
         //双冒号在中间或者没有双冒号
         String regIPv6AbWithColon = "^(" + regHex + "(:|::)){0,6}" + regHex
                 + "$";
        
         //双冒号开头
         String regIPv6AbStartWithDoubleColon = "^(" + "::(" + regHex
                 + ":){0,5}" + regHex + ")$";
        
         String regIPv6 = "^(" + regIPv6Full + ")|("
                 + regIPv6AbStartWithDoubleColon + ")|(" + regIPv6AbWithColon
                 + ")$";

         //System.out.println("regIPv6 =" + regIPv6);
         //regIPv6 =^(^((\p{XDigit}{1,4}):){7}(\p{XDigit}{1,4})$)|(^(::((\p{XDigit}
         //{1,4}):){0,5}(\p{XDigit}{1,4}))$)|(^((\p{XDigit}{1,4})(:|::)){0,6}(\p
         //{XDigit}{1,4})$)$
         //等价于^(^::$)|(^([\\d|a-fA-F]{1,4}:){7}([\\d|a-fA-F]{1,4})$)|(^(::
         //(([\\d|a-fA-F]{1,4}):){0,5}([\\d|a-fA-F]{1,4}))$)|(^(([\\d|a-fA-F]{1,4})
         //(:|::)){0,6}([\\d|a-fA-F]{1,4})$)$
        
         //下面还要处理地址为::的情形和地址包含多于一个的::的情况(非法)
         if (address.indexOf(":") != -1)
         {
             if (address.length() <= 39)
             {
                 String addressTemp = address;
                 int doubleColon = 0;
                 if(address.equals("::"))return true;
                 while (addressTemp.indexOf("::") != -1)
                 {
                     addressTemp = addressTemp.substring(addressTemp
                             .indexOf("::") + 2, addressTemp.length());
                     doubleColon++;
                 }
                 if (doubleColon <= 1)
                 {
                     result = address.matches(regIPv6);
                 }
             }
         }
        return result;
     }
 public static void main(String[] args)
 {
  Hello hello=new Hello();
  boolean bln=false;
  bln=hello.isIPv6("::");
  System.out.println("Start...") ;
  if (bln)
   System.out.println("OK") ;
  else
   System.out.println("NO") ;
 }
}
阅读(4731) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~