国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Regex > 正文

Java正則表達式使用

2020-03-16 21:04:27
字體:
來源:轉載
供稿:網友
本篇文章主要給大家介紹java在正則表達式的使用,本篇文章給大家主要介紹應用點在抓取網頁中的email地址和代碼統計,感興趣的朋友一起看看吧
 

一:抓取網頁中的Email地址

利用正則表達式匹配網頁中的文本

 

復制代碼代碼如下:

[//w[.-]]+@[//w[.-]]+//.[//w]+

 

將網頁內容分割提取
 

  1. import java.io.BufferedReader; 
  2. import java.io.FileNotFoundException; 
  3. import java.io.FileReader; 
  4. import java.io.IOException; 
  5. import java.util.regex.Matcher; 
  6. import java.util.regex.Pattern; 
  7. public class EmailSpider { 
  8.   public static void main(String[] args) { 
  9.     try { 
  10.       BufferedReader br = new BufferedReader(new FileReader("C://emailSpider.html")); 
  11.       String line = ""
  12.       while((line=br.readLine()) != null) { 
  13.         parse(line); 
  14.       } 
  15.     } catch (FileNotFoundException e) { 
  16.       e.printStackTrace(); 
  17.     } catch (IOException e) { 
  18.       e.printStackTrace(); 
  19.     } 
  20.   } 
  21.   private static void parse(String line) { 
  22.     Pattern p = Pattern.compile("[//w[.-]]+@[//w[.-]]+//.[//w]+"); 
  23.     Matcher m = p.matcher(line); 
  24.     while(m.find()) { 
  25.       System.out.println(m.group()); 
  26.     } 
  27.   } 
?

打印結果:

867124664@qq.com
260678675@QQ.com
806208721@qq.com
hr_1985@163.com
32575987@qq.com
qingchen0501@126.com
yingyihanxin@foxmail.com
1170382650@qq.com
1170382650@qq.com
yingyihanxin@foxmail.com
qingchen0501@126.com
32575987@qq.com
hr_1985@163.com

現在你找到這么多郵箱地址,用上JavaMail的知識,你可以群發垃圾郵件了,呵呵!!!

二:代碼統計
 

  1. import java.io.BufferedReader; 
  2. import java.io.File; 
  3. import java.io.FileNotFoundException; 
  4. import java.io.FileReader; 
  5. import java.io.IOException; 
  6. public class CodeCounter { 
  7.   static long normalLines = 0;//正常代碼行 
  8.   static long commentLines = 0;//注釋行 
  9.   static long whiteLines = 0;//空白行 
  10.   public static void main(String[] args) { 
  11.     //找到某個文件夾,該文件夾下面在沒有文件夾,這里沒有寫遞歸處理不在同一文件夾的文件 
  12.     File f = new File("E://Workspaces//eclipse//Application//JavaMailTest//src//com//java//mail"); 
  13.     File[] codeFiles = f.listFiles(); 
  14.     for(File child : codeFiles){ 
  15.       //只統計java文件 
  16.       if(child.getName().matches(".*//.java$")) { 
  17.         parse(child); 
  18.       } 
  19.     } 
  20.     System.out.println("normalLines:" + normalLines); 
  21.     System.out.println("commentLines:" + commentLines); 
  22.     System.out.println("whiteLines:" + whiteLines); 
  23.   } 
  24.   private static void parse(File f) { 
  25.     BufferedReader br = null
  26.     //表示是否為注釋開始 
  27.     boolean comment = false
  28.     try { 
  29.       br = new BufferedReader(new FileReader(f)); 
  30.       String line = ""
  31.       while((line = br.readLine()) != null) { 
  32.         //去掉注釋符/*前面可能出現的空白 
  33.         line = line.trim(); 
  34.         //空行 因為readLine()將字符串取出來時,已經去掉了換行符/n 
  35.         //所以不是"^[//s&&[^//n]]*//n$" 
  36.         if(line.matches("^[//s&&[^//n]]*$")) { 
  37.           whiteLines ++; 
  38.         } else if (line.startsWith("/*") && !line.endsWith("*/")) { 
  39.           //統計多行/*****/ 
  40.           commentLines ++; 
  41.           comment = true;   
  42.         } else if (line.startsWith("/*") && line.endsWith("*/")) { 
  43.           //統計一行/**/ 
  44.           commentLines ++; 
  45.         } else if (true == comment) { 
  46.           //統計*/ 
  47.           commentLines ++; 
  48.           if(line.endsWith("*/")) { 
  49.             comment = false
  50.           } 
  51.         } else if (line.startsWith("//")) { 
  52.           commentLines ++; 
  53.         } else { 
  54.           normalLines ++; 
  55.         } 
  56.       } 
  57.     } catch (FileNotFoundException e) { 
  58.       e.printStackTrace(); 
  59.     } catch (IOException e) { 
  60.       e.printStackTrace(); 
  61.     } finally { 
  62.       if(br != null) { 
  63.         try { 
  64.           br.close(); 
  65.           br = null
  66.         } catch (IOException e) { 
  67.           e.printStackTrace(); 
  68.         } 
  69.       } 
  70.     } 
  71.   } 
?

以上內容就是本文給大家分享的Java在正則表達式的使用,希望大家喜歡。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江达县| 绥棱县| 集贤县| 曲周县| 耿马| 嘉荫县| 河源市| 连云港市| 阿瓦提县| 建宁县| 高青县| 青海省| 娄烦县| 桓台县| 孙吴县| 普兰县| 凯里市| 天等县| 盐城市| 屏边| 昭觉县| 平顶山市| 偃师市| 青河县| 桐庐县| 宜阳县| 中江县| 秀山| 巴塘县| 镇巴县| 广西| 望都县| 民和| 杭州市| 太康县| 神农架林区| 广元市| 鲁甸县| 大连市| 永善县| 宣威市|