java 正则表达

1.验证身份证

public static boolean checkIdentityCard(String identity_card){
		String pattern = "((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)\\d{4})((((19|20)(([02468][048])|([13579][26]))0229))|((20[0-9][0-9])|(19[0-9][0-9]))((((0[1-9])|(1[0-2]))((0[1-9])|(1\\d)|(2[0-8])))|((((0[1,3-9])|(1[0-2]))(29|30))|(((0[13578])|(1[02]))31))))((\\d{3}(x|X))|(\\d{4}))";  
		Pattern p = Pattern.compile(pattern);  
		Matcher m = p.matcher(identity_card);  
		return m.matches();
	}


2.验证ip地址是否同一个段

/**
	 * 验证两个ip是否是同一个段
	 * @param ipStr
	 * @return
	 */
	public boolean checkIp(String ipStr){
		Pattern pattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
		//String ipStr="192.168.1.12/192.168.1.12";
		String[] str=ipStr.split("/");
		if(str.length==2){
			Matcher matcher = pattern.matcher(str[0]);
			Matcher matcher1 = pattern.matcher(str[1]);
			if(matcher.matches()==true&&matcher1.matches()==true){//验证合法
				int i=str[0].lastIndexOf(".");//得到最后一个.在字符串的那个位置
				String ip0_1=str[0].substring(0,i);//前三段
				int ip0_4=Integer.valueOf(str[0].substring(i+1, str[0].length()));
				//System.out.println(ip0_1+"======"+ip0_4);
				int j=str[1].lastIndexOf(".");
				String ip1_1=str[1].substring(0,i);
				int ip1_4=Integer.valueOf(str[1].substring(i+1, str[1].length()));
				//System.out.println(ip1_1+"======"+ip1_4);
				
				if(ip0_1.equals(ip1_1)&&ip0_4<=ip1_4){
					return true;
				}
			}
		}
		return false;
	}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值