日期:2014-05-20 浏览次数:20757 次
public class Test {
public static void main(String[] args) {
// 名字 (长度大于2 首个字符不能为数字)
String regex_s1 = "^[.[^\\d]]{2,}$";
String[] s1 = getS1();
pattern(regex_s1, s1);
// // 性别 (只能是男或则女)
String regex_s2 = "^[男
------解决方案--------------------
女]$";
String[] s2 = getS2();
pattern(regex_s2, s2);
//
// //email (只能是@qq.com结尾的)[这里忽略email命名合法性]
String regex_s3 = "^([.[^\\s]]{1,})(@qq.com)$";
String[] s3 = getS3();
pattern(regex_s3, s3);
// 成绩 (只能是0-100的整数)
String regex_s4 = "[\\d][\\d]?[0]?";
String[] s4 = getS4();
pattern(regex_s4, s4);
}
private static void pattern(String regex, String[] str) {
System.out.println("---------------------------------");
for (int i = 0; i < str.length; i++) {
String s = str[i];
boolean matches = Pattern.matches(regex, s);
System.out.println(regex + ">>" + s + " matches :" + matches);
}
}
private static String[] getS2() {
return new String[] { "男", "女", "男女", "男人", "女人" };
}
private static String[] getS1() {
return new String[] { "隆隆", "小隆隆", "龙", "", "1龙", "1" };
}
private static String[] getS3() {
return new String[] { "sdf@qq.com", "@qq.com", " @qq.com", "", "dd@qq.com", "ss@qq.com2" };
}
private static String[] getS4() {
return new String[] { "50", "-1", "101", "105", "", "1.1", "a", "*", "8", "5" };
}
}