一、简介
正则表达式:可以用一些规定字符来指定规则,并用来校验数据格式的合法性,是一种强大而灵活的文本处理工具。
第一个例子:校验一个qq密码的合法性,要求是6-20位,而且全都得是数字。
- 不使用正则表达式
public class RegexDemo1 {
public static void main(String[] args) {
System.out.println(checkQQpwd("ah")); //false
System.out.println(checkQQpwd("123")); //false
System.out.println(checkQQpwd("123456788901020")); //true
System.out.println(checkQQpwd("12375647477437437473473717213871232717318")); //false
System.out.println(checkQQpwd("128346655667828a9")); //false
}
public static boolean checkQQpwd(String Qq){
//1.判断qq长度
if (Qq == null || Qq.length() < 6 || Qq.length() > 20){
return false;
}
//2.判断是否有字符
for (int i = 0; i < Qq.length(); i++) {
char ch = Qq.charAt(i);
if (ch < '0' || ch > '9'){
return false;
}
}
return true;
}
}
- 使用正则表达式
public class RegexDemo1 {
public static void main(String[] args) {
System.out.println(checkqq2("ah")); //false
System.out.println(checkqq2("123")); //false
System.out.println(checkqq2("123456788901020")); //true
System.out.println(checkqq2("12375647477437437473473717213871232717318")); //false
System.out.println(checkqq2("128346655667828a9")); //false
}
public static boolean checkqq2(String qq){
return qq != null && qq.matches("\\d{6,20}");
}
}
二、正则表达式的匹配规则
调用匹配正则表达式的API:
public boolean matches(String regex):判断是否匹配正则表达式,匹配返回true,不匹配返回false
自己测试一下:
public class RegexDemo2 {
public static void main(String[] args) {
System.out.println("a".matches("[abc]"));
System.out.println("d".matches("[abc]"));
System.out.println("........");
System.out.println("a".matches("[^abc]"));
System.out.println("d".matches("[^abc]"));
System.out.println("........");
System.out.println("1".matches("\\d"));
System.out.println("a".matches("\\d"));
System.out.println("123".matches("\\d"));
System.out.println("123".matches("\\D"));
System.out.println("........");
System.out.println("1".matches("\\w"));
System.out.println("11".matches("\\w"));
System.out.println("11".matches("\\W"));
System.out.println("a".matches("\\w"));
System.out.println("你".matches("\\w"));
System.out.println("你".matches("\\W"));
System.out.println("........");
//要求是数字+字符,6-20位
String qq = "aaaaaa_";
//第一种方式
System.out.println(qq.matches("[a-zA-Z0-9]{6,20}"));
//第二种方式
System.out.println(qq.matches("[\\w&&[^_]]{6,20}"));
}
}
三、自测案例
答案:
public class RegexExample {
public static void main(String[] args) {
phoneFormatCheck();
emailFormatCheck();
telephoneFormatCheck();
}
public static void phoneFormatCheck() {
while (true){
System.out.println("请输入手机号码:");
Scanner scanner = new Scanner(System.in);
String number = scanner.nextLine();
if (number.matches("1[3-9]\\d{9}")){
System.out.println("您输入的号码格式正确");
break;
}else {
System.out.println("号码格式错误,请重新输入");
}
}
}
public static void emailFormatCheck() {
while (true){
System.out.println("请输入邮箱");
Scanner scanner = new Scanner(System.in);
String email = scanner.next();
if (email.matches("\\w{5,30}@[a-zA-Z0-9]{2,20}(\\.[a-zA-Z0-9]{2,20}){1,2}")){
System.out.println("您输入的邮箱格式正确");
break;
}else {
System.out.println("邮箱格式错误,请重新输入");
}
}
}
public static void telephoneFormatCheck() {
while (true){
System.out.println("请输入电话号码");
Scanner scanner = new Scanner(System.in);
String telephone = scanner.next();
if (telephone.matches("0\\d{2,6}-?[6,8]\\d{7}")){
System.out.println("您输入的电话号码格式正确");
break;
}else {
System.out.println("电话号码格式错误,请重新输入");
}
}
}
}
四、方法中的正则表达式
- 字符串方法
实例:
public class RegexInMethods {
public static void main(String[] args) {
String expression1 = "张三sjasajsgs123算算看sahstws哇——sajsy授权";
String expression2 = expression1.replaceAll("\\w+", "讷");
String expression3 = expression1.replaceAll("\\w", "讷");
System.out.println(expression2);
System.out.println(expression3);
String[] strings = expression1.split("\\w+");
System.out.println(Arrays.toString(strings));
}
}
五、正则表达式爬取信息
我们可以在一系列文本当中爬取适当的信息,爬取信息就可以使用正则表达式。
先制定一些正则表达式查询的条件,再通过一个匹配规则的对象(Pattern)去查询,然后通过这个匹配对象去获取匹配信息的对象(Matcher),从而查到相应的信息。
public class InfoMatcher {
public static void main(String[] args) {
String messsages = "我们的学校是张三学院,邮箱是161220372@qq.com,电话是19200000000,地址是020-19273";
String regexPattern = "\\w{1,}@\\w{2,10}(\\.\\w{2,10}){1,2}|" +
"1[3-9]\\d{9}|0\\d{2,3}-?\\d{5,10}";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher = pattern.matcher(messsages);
while (matcher.find()){
System.out.println(matcher.group());
}
}
}
//console
161220372@qq.com
19200000000
020-19273