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的知識,你可以群發(fā)垃圾郵件了,呵呵?。?!
二:代碼統(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();
}
}
}
}
}
以上就是針對java正則表達(dá)式的簡單應(yīng)用,希望對大家的學(xué)習(xí)Java正則表達(dá)式有所幫助。
相關(guān)文章
springboot用戶數(shù)據(jù)修改的詳細(xì)實(shí)現(xiàn)
用戶管理功能作為所有的系統(tǒng)是必不可少的一部分,下面這篇文章主要給大家介紹了關(guān)于springboot用戶數(shù)據(jù)修改的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
MyBatis之foreach標(biāo)簽的用法及多種循環(huán)問題
這篇文章主要介紹了MyBatis之foreach標(biāo)簽的用法及多種循環(huán)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
如何基于SpringWeb?MultipartFile實(shí)現(xiàn)文件上傳、下載功能
在做項(xiàng)目時(shí),后端經(jīng)常采用上傳文件組件MultipartFile,下面這篇文章主要給大家介紹了關(guān)于如何基于SpringWeb?MultipartFile實(shí)現(xiàn)文件上傳、下載功能的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
java使用ArrayList實(shí)現(xiàn)斗地主(無序版)
這篇文章主要為大家詳細(xì)介紹了java使用ArrayList實(shí)現(xiàn)斗地主,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
Java Collections.EMPTY_LIST與Collections.emptyList()的區(qū)別
這篇文章主要介紹了Java Collections.EMPTY_LIST與Collections.emptyList()的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Springboot整合nacos報(bào)錯(cuò)無法連接nacos的解決
這篇文章主要介紹了Springboot整合nacos報(bào)錯(cuò)無法連接nacos的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

