Java中常用的正则表达式判断,如IP地址、电话号码、邮箱等
java中我们会常用一些判断如IP、电子邮箱、电话号码的是不是符合,那么我们怎么来判断呢,答案就是利用正则表达式来判断了,因为本人对正则表达式没有太深的研究,所有感兴趣的朋友可以自行百度。我这就给基本的判断,废话不多说,下面就是上代码。
IP地址的判断方法
1 2 3 4 5 6 7 8 9 | public static boolean orIp(String ip) { if (ip == null || "" .equals(ip)) return false ; String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$" ; return ip.matches(regex); } |
判断是否是正确的邮箱地址
1 2 3 4 5 | public static boolean orEmail(String email) { if (email == null || "" .equals(email)) return false ; String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*" ; return email.matches(regex); } |
判断是否是手机号码
1 2 3 4 5 6 | public static boolean orPhoneNumber(String phoneNumber) { if (phoneNumber == null || "" .equals(phoneNumber)) return false ; String regex = "^1[3|4|5|8][0-9]\\d{8}$" ; return phoneNumber.matches(regex); } |