Regular Expression 校验给定的字符串是否满足某种格式要求(合同) 优点:能够大大的简化代码量; 缺点:阅读性比较差,不能够精确到具体错误、只能测验出有误
一、匹配
1、手机号码校验
普通写法
import java.util.Scanner;
public class Regex1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一个手机号码:");
String phone = input.next();
int len = phone.length();
if (len == 11) {
char[] chs = phone.toCharArray();
if (chs[0] == '1') {
if (chs[1] == '3' || chs[1] == '5' || chs[1] == '7' || chs[1] == '8') {
boolean flag = true;
for (int i = 0; i < chs.length; i++) {
if (!('0' <= chs[i] && chs[i] <= '9')) {
flag = false;
break;
}
}
if (flag) {
System.out.println("亲,手机号码格式正确!!!");
} else {
System.out.println("亲,手机号码必须全部为数字");
}
} else {
System.out.println("亲,你手机号码有误(第2个字母不正确)");
}
} else {
System.out.println("亲,您输入的手机号码有误(第1个字符必须为1)");
}
} else {
System.out.println("亲,您输入的手机号码长度有误!");
}
}
}
使用正则表达式
import java.util.Scanner;
public class Regex2 {
public static void main(String[] args) {
String regex1 = "[1]";
String phone1 = "12";
boolean flag1 = phone1.matches(regex1);
String regex2 = "[3578]";
String phone2 = "2";
boolean flag2 = phone2.matches(regex2);
String regex3 = "[1][3578]";
String phone3 = "1";
boolean flag3 = phone3.matches(regex3);
String regex4 = "[a-zA-Z0-9]";
String phone4 = "N";
boolean flag4 = phone4.matches(regex4);
String regex5 = "[0-9]{9}";
String phone5 = "12345a789";
boolean flag5 = phone5.matches(regex5);
Scanner input = new Scanner(System.in);
System.out.print("请您输入手机号码:");
String phone = input.next();
String regex = "[1][3578][0-9]{9}";
boolean flag = phone.matches(regex);
System.out.println(flag ? "手机号格式正确" : "手机号格式有误,请您重新输入!!!");
}
}
2、QQ号
import java.util.Scanner;
public class Regex3 {
public static void main(String[] args) {
String qq = "1234567891234";
String regex = "[0-9]{5,12}";
boolean flag = qq.matches(regex);
boolean flag1 = "01".matches("[0-9]?");
boolean flag2 = "".matches("[0-9]+");
boolean flag3 = "123".matches("[0-9]*");
System.out.println("flag3=" + flag3);
}
}
3、身份证号
import java.util.Scanner;
public class Regex4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入您的身份证号:");
String idcard = input.next();
boolean flag1 = idcard.matches("[0-9]{17}[0-9X]");
System.out.println("flag1=" + flag1);
}
}
public class Regex5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入您的身份证号:");
String idcard = input.next();
boolean flag1 = idcard.matches("[0-9]{15}([0-9]{2}[0-9X])?");
System.out.println("flag1=" + flag1);
}
}
public class Regex6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入您的身份证号:");
String idcard = input.next();
boolean flag1 = idcard.matches("\\d{15}(\\d{2}[0-9X])?");
System.out.println("flag1=" + flag1);
System.out.println("\\");
}
}
Java的特点:“\\”代表现实中“\”(反,换行符等),“/”无影响(正,HTML).
4、邮箱
使用了RegexTool工具帮助查询正则表达式
import java.util.Scanner;
public class Regex7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入姓名:");
String username = input.next();
boolean flag1 = username.matches("[\\u4e00-\\u9fa5]+");
System.out.println("flag1=" + flag1);
System.out.print("请输入邮箱:");
String email = input.next();
boolean flag2 = email.matches("\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+");
System.out.println("flag2=" + flag2);
}
}
5、测验1
定义一个工具类(RegexTool),工具类中有很多常用的正则方法,方法如下: (1)、isChinese():判断一个字符串是否是中文,接收一个字符串类型参数,返回值类型为boolean (2)、isEmail():判断一个字符串是否是邮箱,接收一个字符串类型参数,返回值类型为boolean (3)、isPhone():判断一个字符串是否是手机号码,接收一个字符串类型参数,返回值类型为boolean (4)、isAge():判断一个字符串是否是年龄(年龄只能为正整数),接收一个字符串类型参数,返回值类型为boolean (5)、isIdCard():判断一个字符串是否是身份证,接收一个字符串类型参数,返回值类型为boolean
要求:方法全部为静态的。
package cn.java.utils;
public class RegexTool {
public static boolean isChinese(String txt) {
String regex = "[\\u4e00-\\u9fa5]+";
if (txt == null) {
System.err.println("亲,字符串不能够为null");
return false;
}
return txt.matches(regex);
}
public static boolean isPhone(String txt) {
if (txt == null) {
return false;
}
return txt.matches("[1][3578]\\d{9}");
}
public static void main(String[] args) {
}
}
Step1:util 放工具类 Step2:把代码粘贴过去,打入名字后alt+/快捷键调出(只有eclipse有代码就有!)
6、测验2
从键盘录入姓名(要求必须为中文)、邮箱、手机号、年龄、身份证号 要求:调用RegexTool类中的方法来校验输入的信息
二、切割
三、替换
|