一、驗證(zheng)郵(you)箱(xiang)是(shi)否正確
public class ForTest {
public static void main(String[] args) {
System.out.println("123");
String email = "gengxiaopeng@hyper-telecom.com";
//boolean ss = email.matches("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$");
System.out.print(email.matches("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"));
}
}
二、驗證密碼的開(kai)始或(huo)結尾(wei)不能為空格
密(mi)碼的開始不(bu)能為空格:password.matches("^\\s+.*");
密碼的結束(shu)不能為空格:password.matches(".*\\s+$")
三、判斷(duan)給(gei)出的日期是(shi)否合法
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ForTest {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^((\\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])))))?$");
Matcher matcher = pattern.matcher("2015-04-30 23:59:59");
System.out.println(matcher.matches());
}
}
四、驗證(zheng)IP地(di)址是否合法:255.255.255.255
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ForTest {
public static void main(String[] args) {
Pattern pattern1 = 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");
Matcher matcher1 = pattern1.matcher("127.400.600.2"); //以驗證127.400.600.2為例
System.out.println(matcher1.matches());
}
}
————————————————
版權聲(sheng)明:本文為(wei)CSDN博主「NeverGiveUp7」的原(yuan)創文章(zhang),遵循CC 4.0 BY-SA版權協議,轉(zhuan)載請(qing)附(fu)上原(yuan)文出處鏈接及本聲(sheng)明。
原文鏈接://blog.csdn.net/gengxiaoming7/article/details/47979281