package com.regex;
import java.util.Scanner;
public class CheckDemo {
public static void main(String[] args) {
// checkPhone();//校验手机号
// checkEmail();//校验邮箱
// checkTel();//校验电话
checkMoney();//校验金额
}
public static void checkTel() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请您输入电话:");
String tel = sc.next();
if (tel.matches("0\\d{2,6}-?\\d{5,20}")) {
System.out.println("电话号格式正确!");
break;
} else {
System.out.println("格式错误,请重新输入!");
}
}
}
public static void checkPhone() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请您输入手机号码:");
String phone = sc.next();
if (phone.matches("1[3-9]\\d{9}")) {
System.out.println("手机号格式正确!");
break;
} else {
System.out.println("格式错误,请重新输入!");
}
}
}
public static void checkEmail() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请您输入邮箱:");
String phone = sc.next();
if (phone.matches("\\w{1,30}@[0-9a-zA-Z]{2,20}(\\.[0-9a-zA-Z]{2,20}){1,2}")) {
System.out.println("邮箱格式正确!");
break;
} else {
System.out.println("格式错误,请重新输入!");
}
}
}
public static void checkMoney() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请您输入金额:");
String money = sc.next();
if (money.matches("\\d{1,}\\.?\\d{1}")) {//
System.out.println("金额格式正确!");
break;
} else {
System.out.println("格式错误,请重新输入!");
}
}
}
}
正则表达式校验手机号,邮箱,电话号码,金额
最新推荐文章于 2024-08-19 09:25:10 发布