java正則表達(dá)式簡單應(yīng)用
一:抓取網(wǎng)頁中的Email地址
利用正則表達(dá)式匹配網(wǎng)頁中的文本
[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+
將網(wǎng)頁內(nèi)容分割提取
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailSpider {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\emailSpider.html"));
String line = "";
while((line=br.readLine()) != null) {
parse(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void parse(String line) {
Pattern p = Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");
Matcher m = p.matcher(line);
while(m.find()) {
System.out.println(m.group());
}
}
}
打印結(jié)果:
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
現(xiàn)在你找到這么多郵箱地址,用上JavaMail的知識(shí),你可以群發(fā)垃圾郵件了,呵呵!?。?/p>
二:代碼統(tǒng)計(jì)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CodeCounter {
static long normalLines = 0;//正常代碼行
static long commentLines = 0;//注釋行
static long whiteLines = 0;//空白行
public static void main(String[] args) {
//找到某個(gè)文件夾,該文件夾下面在沒有文件夾,這里沒有寫遞歸處理不在同一文件夾的文件
File f = new File("E:\\Workspaces\\eclipse\\Application\\JavaMailTest\\src\\com\\java\\mail");
File[] codeFiles = f.listFiles();
for(File child : codeFiles){
//只統(tǒng)計(jì)java文件
if(child.getName().matches(".*\\.java$")) {
parse(child);
}
}
System.out.println("normalLines:" + normalLines);
System.out.println("commentLines:" + commentLines);
System.out.println("whiteLines:" + whiteLines);
}
private static void parse(File f) {
BufferedReader br = null;
//表示是否為注釋開始
boolean comment = false;
try {
br = new BufferedReader(new FileReader(f));
String line = "";
while((line = br.readLine()) != null) {
//去掉注釋符/*前面可能出現(xiàn)的空白
line = line.trim();
//空行 因?yàn)閞eadLine()將字符串取出來時(shí),已經(jīng)去掉了換行符\n
//所以不是"^[\\s&&[^\\n]]*\\n$"
if(line.matches("^[\\s&&[^\\n]]*$")) {
whiteLines ++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
//統(tǒng)計(jì)多行/*****/
commentLines ++;
comment = true;
} else if (line.startsWith("/*") && line.endsWith("*/")) {
//統(tǒng)計(jì)一行/**/
commentLines ++;
} else if (true == comment) {
//統(tǒng)計(jì)*/
commentLines ++;
if(line.endsWith("*/")) {
comment = false;
}
} else if (line.startsWith("http://")) {
commentLines ++;
} else {
normalLines ++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
以上就是針對(duì)java正則表達(dá)式的簡單應(yīng)用,希望對(duì)大家的學(xué)習(xí)Java正則表達(dá)式有所幫助。
相關(guān)文章
SpringCloud Zuul過濾器和谷歌Gauva實(shí)現(xiàn)限流
這篇文章主要介紹了SpringCloud Zuul過濾器和谷歌Gauva實(shí)現(xiàn)限流,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Spring Boot實(shí)戰(zhàn)教程之自動(dòng)配置詳解
Spring Boot的自動(dòng)配置給開發(fā)者帶來了很大的便利,當(dāng)開發(fā)人員在pom文件中添加starter依賴后,maven或者gradle會(huì)自動(dòng)下載很多jar包到classpath中。下面這篇文章主要給大家介紹了關(guān)于Spring Boot自動(dòng)配置的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07
IntelliJ?IDEA的代碼擱置功能實(shí)現(xiàn)
本文主要介紹了IntelliJ?IDEA的代碼擱置功能實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Java?swing實(shí)現(xiàn)應(yīng)用程序?qū)?shù)據(jù)庫的訪問問題
這篇文章主要介紹了Java?swing實(shí)現(xiàn)應(yīng)用程序?qū)?shù)據(jù)庫的訪問,本次實(shí)驗(yàn)需要做一個(gè)GUI界面和一個(gè)連接查詢功能,在論壇上借鑒了其他大佬獲取網(wǎng)站內(nèi)容的部分代碼,然后自己做了一個(gè)及其簡陋的swing界面,算是把這個(gè)實(shí)驗(yàn)完成了,需要的朋友可以參考下2022-09-09
idea的spring boot項(xiàng)目實(shí)現(xiàn)更改端口號(hào)操作
這篇文章主要介紹了idea的spring boot項(xiàng)目實(shí)現(xiàn)更改端口號(hào)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
SpringMVC中Invalid bound statement (not f
本文主要介紹了SpringMVC中Invalid bound statement (not found)常見報(bào)錯(cuò)問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05

