要求:使用 正则表达式 实现以下验证:
匹配Email地址
匹配网址URL
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线)
匹配国内电话号码 区号3位或者4位
匹配国内手机号 11位
匹配腾讯QQ号
匹配中国邮政编码
匹配身份证
匹配ip地址
1.正则主要入口:
/** * */ package com.niit.Regex; import java.util.Scanner; /** * @author: Annie * @date:2016年6月6日 * @description: */ public class RegexHomeWork { public static void main(String[] args) { String flag = null; Scanner in = new Scanner(System.in); RegexFunction rf = new RegexFunction(); int num = 0; do{ System.out.println("-----请执行以下操作:"); System.out.println("1.匹配Email地址"); System.out.println("2.匹配网址:"); System.out.println("3.匹配账号是否合法字母开头,允许5-16字节,允许字母数字下划线:"); System.out.println("4.匹配国内电话号码 区号3位或者4位:"); System.out.println("5.匹配国内手机号 11位"); System.out.println("6.匹配腾讯QQ号"); System.out.println("7.匹配中国邮政编码:"); System.out.println("8.匹配身份证"); System.out.println("9.匹配ip地址"); System.out.println("-------------------"); num = in.nextInt(); switch (num) { case 1: rf.Email(); break; case 2: rf.URL(); break; case 3: rf.accountNumber(); break; case 4: rf.telPhone(); break; case 5: rf.mobilePhone(); break; case 6: rf.qq(); break; case 7: rf.postcode(); break; case 8: rf.iDcard(); break; case 9: rf.ipAdrr(); break; } System.out.println("是否继续:(y/n)"); flag = in.next(); flag = flag.toLowerCase(); }while("y".equals(flag)); } }
2.正则匹配的方法类
/** * */ package com.niit.Regex; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author: Annie * @date:2016年6月6日 * @description:正则表达式匹配的方法 */ public class RegexFunction { Scanner in = new Scanner(System.in); String str,regex; //讲匹配的步骤封装成一个方法 public void patternFunction(String regex,String str){ Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); boolean flag = matcher.find(); if(flag){ System.out.println(matcher.group()+"匹配成功"); }else { System.out.println("匹配错误"); } } public void Email(){ System.out.println("请输入你的邮箱号:"); str = in.next(); regex = "\\w+@\\w+(\\.\\w+)+" ; patternFunction(regex, str); } public void URL(){ // http://www.baidu.com 或者是https://xxxxxxx.xxx.com System.out.println("请输入你的网址:"); str = in.next(); regex = "(http|ftp|https)://(w{3}\\.)*\\w+\\.\\w+"; patternFunction(regex, str); } public void accountNumber(){ System.out.println("请输入你的账号:"); str = in.next(); regex = "[a-zA-Z]\\w{4,15}"; patternFunction(regex, str); } public void telPhone(){ System.out.println("请输入你的电话号码:"); str = in.next(); regex = "\\d{3,4}\\d{8}"; patternFunction(regex, str); } public void mobilePhone(){ System.out.println("请输入你的手机号码:"); str = in.next(); regex = "[1-9][\\d]{10}"; patternFunction(regex, str); } public void qq(){ System.out.println("请输入你的QQ号:"); str = in.next(); regex = "\\w+@\\w+\\.\\w+(\\.\\w+)*"; patternFunction(regex, str); } public void postcode(){ System.out.println("请输入邮政编码"); str = in.next(); regex = "[1-9]\\d{6}"; patternFunction(regex, str); } public void iDcard (){ System.out.println("输入你的身份证号码:"); str = in.next(); regex = "[\\d]{17}\\w"; patternFunction(regex, str); } public void ipAdrr(){ System.out.println("请输入ip地址:"); str = in.next(); regex = "(\\d{1,3}\\.){3}(\\d{1,3})"; patternFunction(regex, str); } }