麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 編程 > Regex > 正文

Java正則表達(dá)式使用

2020-03-16 21:04:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
本篇文章主要給大家介紹java在正則表達(dá)式的使用,本篇文章給大家主要介紹應(yīng)用點(diǎn)在抓取網(wǎng)頁(yè)中的email地址和代碼統(tǒng)計(jì),感興趣的朋友一起看看吧
 

一:抓取網(wǎng)頁(yè)中的Email地址

利用正則表達(dá)式匹配網(wǎng)頁(yè)中的文本

 

復(fù)制代碼代碼如下:

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

 

將網(wǎng)頁(yè)內(nèi)容分割提取
 

  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.   } 
?

打印結(jié)果:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

現(xiàn)在你找到這么多郵箱地址,用上JavaMail的知識(shí),你可以群發(fā)垃圾郵件了,呵呵!!!

二:代碼統(tǒng)計(jì)
 

  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.     //找到某個(gè)文件夾,該文件夾下面在沒(méi)有文件夾,這里沒(méi)有寫(xiě)遞歸處理不在同一文件夾的文件 
  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.       //只統(tǒng)計(jì)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.     //表示是否為注釋開(kāi)始 
  27.     boolean comment = false
  28.     try { 
  29.       br = new BufferedReader(new FileReader(f)); 
  30.       String line = ""
  31.       while((line = br.readLine()) != null) { 
  32.         //去掉注釋符/*前面可能出現(xiàn)的空白 
  33.         line = line.trim(); 
  34.         //空行 因?yàn)閞eadLine()將字符串取出來(lái)時(shí),已經(jīng)去掉了換行符/n 
  35.         //所以不是"^[//s&&[^//n]]*//n$" 
  36.         if(line.matches("^[//s&&[^//n]]*$")) { 
  37.           whiteLines ++; 
  38.         } else if (line.startsWith("/*") && !line.endsWith("*/")) { 
  39.           //統(tǒng)計(jì)多行/*****/ 
  40.           commentLines ++; 
  41.           comment = true;   
  42.         } else if (line.startsWith("/*") && line.endsWith("*/")) { 
  43.           //統(tǒng)計(jì)一行/**/ 
  44.           commentLines ++; 
  45.         } else if (true == comment) { 
  46.           //統(tǒng)計(jì)*/ 
  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.   } 
?

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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 美女黄页网站免费进入 | 日本视频在线播放 | 日韩视频―中文字幕 | 91久久久久久久 | 天天碰夜夜操 | 成人在线视频播放 | 精品麻豆cm视频在线看 | 亚洲黑人在线观看 | 一边吃奶一边摸下娇喘 | 国产成人在线网址 | 他也色在线视频 | 亚洲九九色 | 久久国产精品久久久久 | 亚洲天堂成人在线观看 | 成人一级黄色大片 | 99seav| 欧美一级电影网 | 中文字幕一区二区三区四区 | 97精品国产高清在线看入口 | 久久综合九色综合久久久精品综合 | 中文字幕国 | 一区二区三区四区高清视频 | 成年免费大片黄在线观看岛国 | 激情视频日韩 | 羞羞视频免费网站日本动漫 | 久久久久久中文字幕 | 欧美成人免费tv在线播放 | 毛片视频大全 | 久久久www成人免费毛片 | 国产羞羞视频在线免费观看 | 日本在线播放一区二区三区 | 日本中文字幕网址 | 国产一区网址 | 国产乱淫a∨片免费观看 | 做羞羞视频| 国产精品美女久久久免费 | 天天操天天看 | 精品久久久久久久久久久久久久久久久久久 | 91成人免费版| 亚洲欧美国产高清 | 欧美亚洲国产成人 |