java中使用正则对格式的验证
2014-12-31 23:52
148 查看
import java.util.regex.*;
public final class RegExpValidator {
public static boolean isEmail(String str) {
String regex = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
return match(regex, str);
}
public static boolean isIP(String str) {
String regex = "\\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";
return match(regex, str);
}
@Deprecated
public static boolean IsUrl(String str) {
str = str.toLowerCase();
//String regex = "^(http|www|ftp|https|)?(://)?(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*((:\\d+)?)(/(\\w+(-\\w+)*))*(\\.?(\\w)*)(\\?)?(((\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*(\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*)*(\\w*)*)$";
String regex = "^http://[\\w-\\.]+(?:/|(?:/[\\w\\.\\-]+)*(?:/[\\w\\.\\-]+\\.*))?$";
return match(regex, str);
}
public static boolean isTelephone(String str) {
String regex = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
return match(regex, str);
}
public static boolean isPostalcode(String str) {
String regex = "[1-9]\\d{5}(?!\\d)";
return match(regex, str);
}
public static boolean isIDcard(String str) {
String regex = "(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";
return match(regex, str);
}
public static boolean isDate(String str) {
String regex = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
return match(regex, str);
}
private static boolean match(String regex, String str) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}
public static void main(String[] args) {
System.out.println(RegExpValidator.isEmail("448117419@qq.com"));
System.out.println(RegExpValidator.isIP("192.168.1.123"));
System.out.println(RegExpValidator.isDate("2012-10-10"));
System.out.println(RegExpValidator
.IsUrl("HTTP://www.baidu.nn/hello.do"));
System.out.println(RegExpValidator.isTelephone("13522349251"));
System.out.println(RegExpValidator.isPostalcode("417000"));
System.out.println(RegExpValidator.isIDcard("431221199110020817"));
}
}
public final class RegExpValidator {
public static boolean isEmail(String str) {
String regex = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
return match(regex, str);
}
public static boolean isIP(String str) {
String regex = "\\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";
return match(regex, str);
}
@Deprecated
public static boolean IsUrl(String str) {
str = str.toLowerCase();
//String regex = "^(http|www|ftp|https|)?(://)?(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*((:\\d+)?)(/(\\w+(-\\w+)*))*(\\.?(\\w)*)(\\?)?(((\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*(\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*)*(\\w*)*)$";
String regex = "^http://[\\w-\\.]+(?:/|(?:/[\\w\\.\\-]+)*(?:/[\\w\\.\\-]+\\.*))?$";
return match(regex, str);
}
public static boolean isTelephone(String str) {
String regex = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
return match(regex, str);
}
public static boolean isPostalcode(String str) {
String regex = "[1-9]\\d{5}(?!\\d)";
return match(regex, str);
}
public static boolean isIDcard(String str) {
String regex = "(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";
return match(regex, str);
}
public static boolean isDate(String str) {
String regex = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
return match(regex, str);
}
private static boolean match(String regex, String str) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}
public static void main(String[] args) {
System.out.println(RegExpValidator.isEmail("448117419@qq.com"));
System.out.println(RegExpValidator.isIP("192.168.1.123"));
System.out.println(RegExpValidator.isDate("2012-10-10"));
System.out.println(RegExpValidator
.IsUrl("HTTP://www.baidu.nn/hello.do"));
System.out.println(RegExpValidator.isTelephone("13522349251"));
System.out.println(RegExpValidator.isPostalcode("417000"));
System.out.println(RegExpValidator.isIDcard("431221199110020817"));
}
}
相关文章推荐
- C#中使用正则表达式验证Email格式、IP地址格式和URL网址格式
- 最全的Java中正则表达式验证日期格式
- Swift - 正则表达式的使用(附用户名、邮箱、URL等常用格式验证)
- Java中Calendar类的简单使用方法,以及日期格式化的方法,字符串日期格式的验证
- java正则表达式验证格式
- 正则表达式 - java验证IP地址 - java验证日期时间,解决润月 - java验证邮箱格式
- java使用正则表达式的方法从json格式的字符串中提取需要的字符串
- Java中正则表达式的使用以及android邮箱格式判断
- java正则表达式的验证和使用实例(含SSH)
- Android 使用正则表达式验证邮箱格式是否正确
- 使用正则表达式验证Email的格式
- 在C#中使用正则表达式验证数据格式
- java验证格式(正则表达式)
- Java简单注册格式验证(正则表达式)
- Java中正则表达式验证日期格式
- java正则表达式验证IP,日期和邮箱格式有效性
- 正则验证使用方法(java和js)
- java 使用正则验证
- Java使用正则表达式对注册页面进行验证功能实现
- Java代码使用正则验证和常用工具方法