首页新闻找找看学习计划

校验地址问题,谁帮找找原因?

0
悬赏园豆:40 [已解决问题] 解决于 2013-07-11 09:24
  1 //==============================================================================
  2 //
  3 //    @author Slive
  4 //    @date  2013-6-18
  5 //
  6 //==============================================================================
  7 package org.slive.validator;
  8 
  9 import java.util.regex.Matcher;
 10 import java.util.regex.Pattern;
 11 
 12 /**
 13  * ValidatorUtil supplies many ordinary validation functions,such as:<br/>
 14  * <ul>
 15  *     <li> ipv4 address validate
 16  *     <li> device mac address validate
 17  * </ul>
 18  * @author Slive
 19  */
 20 public class ValidateUtil
 21 {
 22     public static final String IPV4_REGEX = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
 23     public static final String MAC_REGEX = "([0-9A-Fa-f]{2})(-[0-9A-Fa-f]{2}){5}";
 24     public static final String R_1_65535_REGEX = "([1-9](\\d{0,3}))|([1-5](\\d{4}))|(6553[0-5])|(655[0-2]\\d)|(65[0-4](\\d{2}))|(6[0-4](\\d{3}))";
 25     public static final String IPV4_PORT_REGEX = IPV4_REGEX + "(\\/)" +  R_1_65535_REGEX;
 26     public static final String UDP_IPV4_PORT_REGEX = "([uU][dD][pP])(\\:)" + IPV4_PORT_REGEX;
 27     public static final String TCP_IPV4_PORT_REGEX = "([tT][cC][pP])(\\:)" + IPV4_PORT_REGEX;
 28     
 29     /**
 30      * Legal ipv4 address follow as: "xxx(1-255).xxx.xxx.xxx(0-255)"
 31      * @param ipv4 ipv4 address
 32      * @return    if ipv4 is null, it will return false;
 33      * @see #IP_REGEX
 34      */
 35     public static boolean ipV4Validate(String ipv4){
 36         return ipv4Validate(ipv4,IPV4_REGEX);
 37     }
 38     
 39     /**
 40      * Legal device mac address follow as: "xx-xx-xx-xx-xx(0-9a-f)"
 41      * @param mac device mac address
 42      * @return    if mac is null, it will return false;
 43      * @see #MAC_REGEX
 44      */
 45     public static boolean macValidate(String mac){
 46         return ipv4Validate(mac,MAC_REGEX);
 47     }
 48     
 49     /**
 50      * Legal address follow as:"udp:127.0.0.1/1080"
 51      * @param udpIpv4Port
 52      * @see #UDP_IPV4_PORT_REGEX
 53      */
 54     public static boolean udpIpv4PortValidate(String udpIpv4Port){
 55         return ipv4Validate(udpIpv4Port,UDP_IPV4_PORT_REGEX);
 56     }
 57     
 58     /**
 59      * Legal address follow as:"tcp:127.0.0.1/1080"
 60      * @param tcpIpv4Port
 61      * @see #TCP_IPV4_PORT_REGEX
 62      */
 63     public static boolean tcpIpv4PortValidate(String tcpIpv4Port){
 64         return ipv4Validate(tcpIpv4Port,TCP_IPV4_PORT_REGEX);
 65     }
 66     
 67     /**
 68      * Legal address follow as:"127.0.0.1/1080"
 69      * @param ipv4Port
 70      * @see #IPV4_PORT_REGEX
 71      */
 72     public static boolean ipv4PortValidate(String ipv4Port){
 73         return ipv4Validate(ipv4Port,IPV4_PORT_REGEX);
 74     }
 75 
 76     private static boolean ipv4Validate(String addr,String regex) {
 77         if(addr == null){
 78             return false;
 79         }
 80         else{
 81             Pattern p = Pattern.compile(regex);
 82             Matcher m = p.matcher(addr);
 83             boolean b = m.matches();
 84             return b;
 85 //            return Pattern.matches(regex, addr.trim());
 86         }
 87     }
 88     
 89     public static void main(String[] args) {
 90         for (int i = 0; i < 65537; i++) {
 91             if(!("" + i).matches(R_1_65535_REGEX))        // 校验是否是1-65535的范围内
 92             {
 93                 System.out.println(i);
 94             }
 95         }
 96         
 97         // TODO
 98         System.out.println();
 99         String udpAddr = "udp:127.0.0.1/655";
100 //        String udpAddr = "udp:127.0.0.1/65535";  //此地址会出错,y?
101         System.out.println("UdpAddr validates result:" + udpIpv4PortValidate(udpAddr));
102         
103         String tcpAddr = "tcp:10.8.9.116/10000";
104         System.out.println("TcpAddr validates result:" + tcpIpv4PortValidate(tcpAddr));
105     }
106     
107 }

如上代码,测试检验是否形如“udp|tcp:xxx.xxx.xxx.xxx/xxx(1-65535)”等的地址是否是有效的,检验"udp:127.0.0.1/655"时没问题,检验"udp:127.0.0.1/65535"时却出错了(单单测试检验1-65535是否有效却没问题),哪位大侠帮忙找找原因?

_Slive的主页 _Slive | 初学一级 | 园豆:166
提问于:2013-07-10 16:44
< >
分享
最佳答案
0

public static final String R_1_65535_REGEX = "([1-9](\\d{0,3}))|([1-5](\\d{4}))|(6553[0-5])|(655[0-2]\\d)|(65[0-4](\\d{2}))|(6[0-4](\\d{3}))";
在最外面应该加一个括号吧,“|”运算包括到前面的地址了。

收获园豆:20
kylin.chen | 小虾三级 |园豆:983 | 2013-07-10 18:11

ok了,3Q

_Slive | 园豆:166 (初学一级) | 2013-07-11 09:23
其他回答(1)
0
第一要改的: IPV4_PORT_REGEX = IPV4_REGEX + "(\\/)(" +  R_1_65535_REGEX+")";
第二要改的:
R_1_65535_REGEX="([1-5](\\d{4}))|(6553[0-5])|(655[0-2]\\d)|(65[0-4](\\d{2}))|(6[0-4](\\d{3}))|([1-9](\\d{0,3}))";
收获园豆:20
Yu | 园豆:12944 (专家六级) | 2013-07-10 22:01

ok了,3Q

支持(0) 反对(0) _Slive | 园豆:166 (初学一级) | 2013-07-11 09:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册