应用案例
请使用正则表达式完成如下需求:
校验用户输入的电话、邮箱是否合法
校验电话
import java.util.Scanner; public class Regextest { public static void main(String[] args) { checkPhone(); } public static void checkPhone(){ while(true){ System.out.println("请您输入您的电话号码(手机|座机):"); Scanner sc = new Scanner(System.in); String phone = sc.nextLine(); //接收一行数据 //手机号码正确格式:13437542098 //座机号码正确格式:010-3424242424 || 0104665354 if(phone.matches("(1[3-9]\\d{9})|(0\\d{2,7}-?[1-9]\\d{4,19})")){ System.out.println("您输入的号码格式正确"); break; }else{ System.out.println("您输入的号码格式不正确!"); } } } }
运行结果:
校验邮箱
import java.util.Scanner; public class Regextest { public static void main(String[] args) { checkEmail(); } public static void checkEmail(){ while(true){ System.out.println("请您输入您的邮箱:"); Scanner sc = new Scanner(System.in); String email = sc.nextLine(); /* * okokok009@163.com * 2668834948@qq.com * morefun@lemon.com.cn */ if(email.matches("\\w{2,}@\\w{2,20}(\\.\\w{2,10}){1,2}")){ System.out.println("您输入的邮箱格式正确"); break; }else{ System.out.println("您输入的邮箱格式不正确!"); } } } }
运行结果:
查找信息
- 使用正则表达式查找一段文本中的内容
需求:从以下内容中爬取出,手机,邮箱,座机、400电话等信息。
"程序员学习Java,\n" +
"电话:1866668888,18699997777\n" +
"或者联系邮箱:2668834948@qq.com,\n" +
"座机电话:01036517895,010-98951256\n” +
"邮箱:buzhi@itopen.cn,\n" +
"邮箱:dLei0009@163.com,\n” +
"热线电话:400-618-9090,400-618-4000,4006184000,4006189090";
(为固定代码,可以简单记住)
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Regextest2 { public static void main(String[] args) { String data = "程序员学习Java,\n" + "电话:1866668888,18699997777\n" + "或者联系邮箱:2668834948@qq.com,\n" + "座机电话:01036517895,010-98951256\n" + "邮箱:buzhi@itopen.cn,\n" + "邮箱:dLei0009@163.com,\n" + "热线电话:400-618-9090,400-618-4000,4006184000,4006189090"; //1.定义爬取规则 String regex = "(1[3-9]\\d{9})|(0\\d{2,7}-?[1-9]\\d{4,19})|\\w{2,}@\\w{2,20}(\\.\\w{2,10}){1,2}" + "|(400-?\\d{3,7}-?\\d{3,7})"; //2.把正则表达式封装成一个Pattern对象 Pattern pattern = Pattern.compile(regex); //3.通过Pattern对象去获取查找内容的匹配器对象 Matcher matcher = pattern.matcher(data); //4.定义一个循环开始爬取信息 while(matcher.find()){ String rs = matcher.group(); //获取到了找到的内容了 System.out.println(rs); } } }
运行结果:
用于搜索替换、分割内容
正则表达式用于搜索替换、分割内容,需要结合String提供的如下方法完成:
搜索替换
需求1:请把古力娜扎ai8888迪丽热巴999aa5566马尔扎哈fbbfsfs42425卡尔扎巴,中间的非中文字符替换成"_"
public class Regextest3 { public static void main(String[] args) { //1.public String repLaceALL(String regex,String newStr):按照正则表达式匹配的内容进行替换 //需求1:请把古力娜扎ai8888迪丽热巴999aa5566马尔扎哈fbbfsfs42425卡尔扎巴,中间的非中文字符替换成"_" String s1 = "古力娜扎ai8888迪丽热巴999aa5566马尔扎哈fbbfsfs42425卡尔扎巴"; System.out.println(s1.replaceAll("\\w+", "-")); } }
运行结果:
需求2(拓展):某语音系统,收到一个口吃的人说的"我我我喜欢编编编编编编编编编编编编程程程!”,需要优化成"我喜欢编程!"
package user.RegularExpression; public class Regextest3 { public static void main(String[] args) { //需求2(拓展):某语音系统,收到一个口吃的人说的"我我我喜欢编编编编编编编编编编编编程程程!”,需要优化成"我喜欢编程!" String s2 = "我我我喜欢编编编编编编编编编编编编程程程!"; /* * (.)一组: .匹配任意字符 * \\1: 为这个组声明一个组号:1号 * +: 声明必须是重复的字 * $1 可以去取到第1组代表的那个重复的字 */ System.out.println(s2.replaceAll("(.)\\1+", "$1")); } }
运行结果:
分割内容
需求:请把古力娜扎ai8888迪丽热巴999aa5566马尔扎哈fbbfsfs42425卡尔扎巴,中的人名获取出来。
package user.RegularExpression; import java.util.Arrays; public class Regextest3 { public static void main(String[] args) { //2.public String[] spLit(String regex):按照正则表达式匹配的内容进行分割字符串,反回一个字符串数组。 String s3 = "古力娜扎ai8888迪丽热巴999aa5566马尔扎哈fbbfsfs42425卡尔扎巴"; String[] names = s3.split("\\w+"); System.out.println(Arrays.toString(names)); } }
运行结果:
END