Java常用工具类三 -- 正则表达式

来源:互联网 发布:sql insert case when 编辑:程序博客网 时间:2024/05/21 09:04

1.抓取网页email地址实例

实现思路:

1、使用java.net.URL对象,绑定网络上某一个网页的地址

2、通过java.net.URL对象的openConnection()方法获得一个HttpConnection对象

3、通过HttpConnection对象的getInputStream()方法获得该网络文件的输入流对象InputStream

4、循环读取流中的每一行数据,并由Pattern对象编译的正则表达式区配每一行字符,取得email地址

[java] view plaincopy
  1. package regex;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.InputStreamReader;  
  5. import java.net.URL;  
  6. import java.net.URLConnection;  
  7. import java.util.regex.Matcher;  
  8. import java.util.regex.Pattern;  
  9.   
  10. /** 
  11.  * 网络爬虫,抓取网页中的email地址 
  12.  */  
  13. public class WebCrawlersDemo {  
  14.       
  15.     public static void main(String[] args) throws Exception {  
  16.         URL url = new URL("http://www.tianya.cn/publicforum/content/english/1/129176.shtml");  
  17.         // 打开连接  
  18.         URLConnection conn = url.openConnection();  
  19.         // 设置连接网络超时时间  
  20.         conn.setConnectTimeout(1000 * 10);  
  21.         // 读取指定网络地址中的文件  
  22.         BufferedReader bufr = new BufferedReader(new InputStreamReader(conn.getInputStream()));  
  23.         String line = null;  
  24.         String regex = "[a-zA-Z0-9_-]+@\\w+\\.[a-z]+(\\.[a-z]+)?";  // 匹配email的正则  
  25.         Pattern p = Pattern.compile(regex);  
  26.         while((line = bufr.readLine()) != null) {  
  27.             Matcher m = p.matcher(line);  
  28.             while(m.find()) {  
  29.                 System.out.println(m.group());<span style="white-space:pre">    </span>// 获得匹配的email  
  30.             }  
  31.         }  
  32.     }  
  33.   
  34. }  
结果:




2.常用正则工具类  --验证手机,身份证,邮箱等


 此类提供日常开发中常用的正则验证函数,比如:邮箱、手机号、电话号码、身份证号码、日期、数字、小数、URL、IP地址等。使用Pattern对象的matches方法进行整个字符匹配,调用该方法相当于: 

        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(input);
        return m.matches();

每个正则可能还有待优化的地方,您如有更好的方式实现某一个功能的验证,欢迎提出来大家一起讨论。下面是工具类的完整代码:

[java] view plaincopy
  1. /** 
  2.  * 正则工具类 
  3.  * 提供验证邮箱、手机号、电话号码、身份证号码、数字等方法 
  4.  */  
  5. public final class RegexUtils {  
  6.   
  7.     /** 
  8.      * 验证Email 
  9.      * @param email email地址,格式:zhangsan@sina.com,zhangsan@xxx.com.cn,xxx代表邮件服务商 
  10.      * @return 验证成功返回true,验证失败返回false 
  11.      */  
  12.     public static boolean checkEmail(String email) {  
  13.         String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";  
  14.         return Pattern.matches(regex, email);  
  15.     }  
  16.       
  17.     /** 
  18.      * 验证身份证号码 
  19.      * @param idCard 居民身份证号码15位或18位,最后一位可能是数字或字母 
  20.      * @return 验证成功返回true,验证失败返回false 
  21.      */  
  22.     public static boolean checkIdCard(String idCard) {  
  23.         String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";  
  24.         return Pattern.matches(regex,idCard);  
  25.     }  
  26.       
  27.     /** 
  28.      * 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港)) 
  29.      * @param mobile 移动、联通、电信运营商的号码段 
  30.      *<p>移动的号段:134(0-8)、135、136、137、138、139、147(预计用于TD上网卡) 
  31.      *、150、151、152、157(TD专用)、158、159、187(未启用)、188(TD专用)</p> 
  32.      *<p>联通的号段:130、131、132、155、156(世界风专用)、185(未启用)、186(3g)</p> 
  33.      *<p>电信的号段:133、153、180(未启用)、189</p> 
  34.      * @return 验证成功返回true,验证失败返回false 
  35.      */  
  36.     public static boolean checkMobile(String mobile) {  
  37.         String regex = "(\\+\\d+)?1[3458]\\d{9}$";  
  38.         return Pattern.matches(regex,mobile);  
  39.     }  
  40.       
  41.     /** 
  42.      * 验证固定电话号码 
  43.      * @param phone 电话号码,格式:国家(地区)电话代码 + 区号(城市代码) + 电话号码,如:+8602085588447 
  44.      * <p><b>国家(地区) 代码 :</b>标识电话号码的国家(地区)的标准国家(地区)代码。它包含从 0 到 9 的一位或多位数字, 
  45.      *  数字之后是空格分隔的国家(地区)代码。</p> 
  46.      * <p><b>区号(城市代码):</b>这可能包含一个或多个从 0 到 9 的数字,地区或城市代码放在圆括号—— 
  47.      * 对不使用地区或城市代码的国家(地区),则省略该组件。</p> 
  48.      * <p><b>电话号码:</b>这包含从 0 到 9 的一个或多个数字 </p> 
  49.      * @return 验证成功返回true,验证失败返回false 
  50.      */  
  51.     public static boolean checkPhone(String phone) {  
  52.         String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";  
  53.         return Pattern.matches(regex, phone);  
  54.     }  
  55.       
  56.     /** 
  57.      * 验证整数(正整数和负整数) 
  58.      * @param digit 一位或多位0-9之间的整数 
  59.      * @return 验证成功返回true,验证失败返回false 
  60.      */  
  61.     public static boolean checkDigit(String digit) {  
  62.         String regex = "\\-?[1-9]\\d+";  
  63.         return Pattern.matches(regex,digit);  
  64.     }  
  65.       
  66.     /** 
  67.      * 验证整数和浮点数(正负整数和正负浮点数) 
  68.      * @param decimals 一位或多位0-9之间的浮点数,如:1.23,233.30 
  69.      * @return 验证成功返回true,验证失败返回false 
  70.      */  
  71.     public static boolean checkDecimals(String decimals) {  
  72.         String regex = "\\-?[1-9]\\d+(\\.\\d+)?";  
  73.         return Pattern.matches(regex,decimals);  
  74.     }   
  75.       
  76.     /** 
  77.      * 验证空白字符 
  78.      * @param blankSpace 空白字符,包括:空格、\t、\n、\r、\f、\x0B 
  79.      * @return 验证成功返回true,验证失败返回false 
  80.      */  
  81.     public static boolean checkBlankSpace(String blankSpace) {  
  82.         String regex = "\\s+";  
  83.         return Pattern.matches(regex,blankSpace);  
  84.     }  
  85.       
  86.     /** 
  87.      * 验证中文 
  88.      * @param chinese 中文字符 
  89.      * @return 验证成功返回true,验证失败返回false 
  90.      */  
  91.     public static boolean checkChinese(String chinese) {  
  92.         String regex = "^[\u4E00-\u9FA5]+$";  
  93.         return Pattern.matches(regex,chinese);  
  94.     }  
  95.       
  96.     /** 
  97.      * 验证日期(年月日) 
  98.      * @param birthday 日期,格式:1992-09-03,或1992.09.03 
  99.      * @return 验证成功返回true,验证失败返回false 
  100.      */  
  101.     public static boolean checkBirthday(String birthday) {  
  102.         String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";  
  103.         return Pattern.matches(regex,birthday);  
  104.     }  
  105.       
  106.     /** 
  107.      * 验证URL地址 
  108.      * @param url 格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或 http://www.csdn.net:80 
  109.      * @return 验证成功返回true,验证失败返回false 
  110.      */  
  111.     public static boolean checkURL(String url) {  
  112.         String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?";  
  113.         return Pattern.matches(regex, url);  
  114.     }  
  115.       
  116.     /** 
  117.      * 匹配中国邮政编码 
  118.      * @param postcode 邮政编码 
  119.      * @return 验证成功返回true,验证失败返回false 
  120.      */  
  121.     public static boolean checkPostcode(String postcode) {  
  122.         String regex = "[1-9]\\d{5}";  
  123.         return Pattern.matches(regex, postcode);  
  124.     }  
  125.       
  126.     /** 
  127.      * 匹配IP地址(简单匹配,格式,如:192.168.1.1,127.0.0.1,没有匹配IP段的大小) 
  128.      * @param ipAddress IPv4标准地址 
  129.      * @return 验证成功返回true,验证失败返回false 
  130.      */  
  131.     public static boolean checkIpAddress(String ipAddress) {  
  132.         String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))";  
  133.         return Pattern.matches(regex, ipAddress);  
  134.     }  
  135.       
  136. }  

单元测试类完整代码:

[java] view plaincopy
  1. /** 
  2.  * 正则表达式工具类测试 
  3.  */  
  4. public class RegexUtilsTest {  
  5.       
  6.     /** 
  7.      * 验证邮箱 
  8.      */  
  9.     @Test  
  10.     public void testCheckEmail() {  
  11.         boolean result = RegexUtils.checkEmail("zha2_ngsan@sina.com.cn");  
  12.         Assert.assertTrue(result);  
  13.     }  
  14.       
  15.     /** 
  16.      * 验证身份证号码 
  17.      */  
  18.     @Test  
  19.     public void testCheckIdCard() {  
  20.         boolean result = RegexUtils.checkIdCard("432403193902273273");  
  21.         Assert.assertTrue(result);  
  22.     }  
  23.       
  24.     /** 
  25.      * 验证手机号码 
  26.      */  
  27.     @Test  
  28.     public void testCheckMobile() {  
  29.         boolean result = RegexUtils.checkMobile("+8613620285733");  
  30.         Assert.assertTrue(result);  
  31.     }  
  32.       
  33.     /** 
  34.      * 验证电话号码 
  35.      */  
  36.     @Test  
  37.     public void testCheckPhone() {  
  38.         boolean result = RegexUtils.checkPhone("+860738-4630706");  
  39.         Assert.assertTrue(result);  
  40.     }  
  41.       
  42.     /** 
  43.      * 验证整数(正整数和负整数) 
  44.      */  
  45.     @Test  
  46.     public void testCheckDigit() {  
  47.         boolean result = RegexUtils.checkDigit("123132");  
  48.         Assert.assertTrue(result);  
  49.     }  
  50.       
  51.     /** 
  52.      * 验证小数和整数(正负整数和正负小数) 
  53.      */  
  54.     @Test  
  55.     public void testCheckDecimals() {  
  56.         boolean result = RegexUtils.checkDecimals("-33.2");  
  57.         Assert.assertTrue(result);  
  58.     }  
  59.       
  60.     /** 
  61.      * 验证空白字符 
  62.      */  
  63.     @Test  
  64.     public void testCheckBlankSpace() {  
  65.         boolean result = RegexUtils.checkBlankSpace("           ");  
  66.         Assert.assertTrue(result);  
  67.     }  
  68.       
  69.     /** 
  70.      * 匹配中文 
  71.      */  
  72.     @Test  
  73.     public void testCheckChinese() {  
  74.         boolean result = RegexUtils.checkChinese("中文");  
  75.         Assert.assertTrue(result);  
  76.     }  
  77.       
  78.     /** 
  79.      * 验证日期 
  80.      */  
  81.     @Test  
  82.     public void testCheckBirthday() {  
  83.         boolean result = RegexUtils.checkBirthday("1992/09/03");  
  84.         Assert.assertTrue(result);  
  85.     }  
  86.       
  87.     /** 
  88.      * 验证中国邮政编码 
  89.      */  
  90.     @Test  
  91.     public void testCheckPostcode() {  
  92.         boolean result = RegexUtils.checkPostcode("417100");  
  93.         Assert.assertTrue(result);  
  94.     }  
  95.       
  96.     /** 
  97.      * 验证URL地址 
  98.      */  
  99.     @Test  
  100.     public  void testCheckURL() {  
  101.         boolean result = RegexUtils.checkURL("http://blog.csdn.com:80/xyang81/article/details?name=&abc=中文");  
  102.         Assert.assertTrue(result);  
  103.     }  
  104.       
  105.     /** 
  106.      * 验证IP地址 
  107.      */  
  108.     @Test  
  109.     public void testCheckIpAddress() {  
  110.         boolean result = RegexUtils.checkIpAddress("192.1.22.255");  
  111.         Assert.assertTrue(result);  
  112.     }  
  113. }  

测试结果:




3.代码量统计工具(统计java源文件中注释、代码、空白行数量)


比如想统计一个Java程序员一天写代码的工作量(如:有效代码多少行、空行多少、注释多少行等),这个小工具也许能做为一个参考的依据。

    思路:因为每个java源文件的内容基本包括java语句、空白行、注释三部份组成(不包括注解),所以要统计某个文件这三部份的内容各占多少时,只需写三个匹配这几部份内容的正则表达式即可。然后通过IO流读取文件中的每一行,并根据正则匹配的结果,累加每部份匹配的数量即可。

    注释行:单行注释(//)、多行注释、文档注释。正则:((//)|(/\\*+)|((^\\s)*\\*)|((^\\s)*\\*+/))+

    空白行:一行内只有空格、\t、\n等非可视字符表示空白行。正则:^\\s*$

    代码行:以分号“;“结束为一行有效的代码。正则:(?!import|package).+;\\s*(((//)|(/\\*+)).*)*

    实现方式:

        1、用户在控制台输入要统计的java文件路径或目录

        2、根据路径创建一个File对象,判断文件是否有效,并给出提示。最后将文件和IO流绑定

        3、循环读取文件中的某一行,并根据正则匹配的结果累记每部份匹配的数量


完整代码如下:
[java] view plaincopy
  1. package regex;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileFilter;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileNotFoundException;  
  8. import java.io.IOException;  
  9. import java.io.InputStreamReader;  
  10. import java.util.Scanner;  
  11. import java.util.regex.Pattern;  
  12.   
  13. /** 
  14.  * 代码统计工具 
  15.  * 统计某个java源文件或某个目录中所有java源文件中注释、空白行、代码行各占多少行 
  16.  * 注释:包括单行注释(//)、多行注释、文档注释 
  17.  * 空白行:一行内没有任何内容的表示空白行 
  18.  * 代码行:以分号“;”结束的一条语句,可以统计为一行有效代码 
  19.  */  
  20. public class CodeStatDemo {  
  21.       
  22.     // 记录注释行数  
  23.     static long annotationLine = 0;  
  24.       
  25.     // 记录空白行数  
  26.     static long blankLine = 0;  
  27.       
  28.     // 记录有效代码的行数  
  29.     static long codeLine = 0;  
  30.       
  31.     // 代码总行数  
  32.     static long totalLine = 0;  
  33.       
  34.     // 文件总数  
  35.     static long fileCount = 0;  
  36.   
  37.     public static void main(String[] args) throws FileNotFoundException  {  
  38.         System.out.println("请输入要统计代码量的java文件或java目录:");  
  39.         Scanner in = new Scanner(System.in);  
  40.         String filePath = in.nextLine();  
  41.           
  42.         File file = new File(filePath);  
  43.         // 根据用户输入的文件名和目录执行代码量统计  
  44.         codeStat(file);  
  45.           
  46.         System.out.println("----------统计结果---------");  
  47.         System.out.println("文件数量:" + fileCount + "个");  
  48.         System.out.println(file + "文件/目录总行数:" + totalLine);  
  49.         System.out.println("代码行数:" + codeLine);  
  50.         System.out.println("注释行数:" + annotationLine);  
  51.         System.out.println("空白行数:" + blankLine);  
  52.         long otherLine = totalLine - (codeLine + annotationLine + blankLine);  
  53.         System.out.println("其它行数:" + otherLine);  
  54.           
  55.     }  
  56.   
  57.     private static void codeStat(File file) throws FileNotFoundException {  
  58.         if (file == null || !file.exists())   
  59.             throw new FileNotFoundException(file + ",文件不存在!");  
  60.           
  61.         fileCount ++;   // 文件数累加  
  62.           
  63.         if (file.isDirectory()) {  
  64.             File[] files = file.listFiles(new FileFilter() {  
  65.                   
  66.                 @Override  
  67.                 public boolean accept(File pathname) {  
  68.                     return pathname.getName().endsWith(".java") || pathname.isDirectory();  
  69.                 }  
  70.             });  
  71.               
  72.             for (File target : files) {  
  73.                 codeStat(target);  
  74.             }  
  75.         } else {  
  76.             BufferedReader bufr = null;  
  77.             try {  
  78.                 // 将指定路径的文件与字符流绑定  
  79.                 bufr = new BufferedReader(new InputStreamReader(new FileInputStream(file)));  
  80.             } catch (FileNotFoundException e) {  
  81.                 throw new FileNotFoundException(file + ",文件不存在!" + e);  
  82.             }  
  83.               
  84.             // 定义匹配每一行的正则匹配器  
  85.             Pattern annotationLinePattern = Pattern.compile("((//)|(/\\*+)|((^\\s)*\\*)|((^\\s)*\\*+/))+",   
  86.                     Pattern.MULTILINE + Pattern.DOTALL);    // 注释匹配器(匹配单行、多行、文档注释)  
  87.               
  88.             Pattern blankLinePattern = Pattern.compile("^\\s*$");   // 空白行匹配器(匹配回车、tab键、空格)  
  89.               
  90.             Pattern codeLinePattern = Pattern.compile("(?!import|package).+;\\s*(((//)|(/\\*+)).*)*",  
  91.                     Pattern.MULTILINE + Pattern.DOTALL); // 代码行匹配器(以分号结束为一行有效语句,但不包括import和package语句)  
  92.               
  93.             // 遍历文件中的每一行,并根据正则匹配的结果记录每一行匹配的结果  
  94.             String line = null;  
  95.             try {  
  96.                 while((line = bufr.readLine()) != null) {  
  97.                     if (annotationLinePattern.matcher(line).find()) {  
  98.                         annotationLine ++;  
  99.                     }  
  100.                       
  101.                     if (blankLinePattern.matcher(line).find()) {  
  102.                         blankLine ++;  
  103.                     }  
  104.                       
  105.                     if (codeLinePattern.matcher(line).matches()) {  
  106.                         codeLine ++;  
  107.                     }   
  108.                       
  109.                     totalLine ++;  
  110.                 }  
  111.                   
  112.             } catch (IOException e) {  
  113.                 throw new RuntimeException("读取文件失败!" + e);  
  114.             } finally {  
  115.                 try {  
  116.                     bufr.close();   // 关闭文件输入流并释放系统资源  
  117.                 } catch (IOException e) {  
  118.                     throw new RuntimeException("关闭文件输入流失败!");  
  119.                 }  
  120.             }  
  121.         }  
  122.     }  
  123. }  
测试结果:

0 0
原创粉丝点击