利用正则表达式统计代码中的(代码行数,注释行数,空白行数)利用正则表达式获取一个网页中所有的邮箱地址

正则表达式的简单应用:

利用正则表达式统计代码中的(代码行数,注释行数,空白行数)。利用正则表达式获取一个网页中所有的邮箱地址

利用正则表达式统计代码中的(代码行数,注释行数,空白行数)

package cn.kpchen.five;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 利用正则表达式统计代码中的(代码行数,注释行数,空白行数)
 * 
 * @desc: regulardemo
 * @author: kpchen
 * @createTime: 2014年11月8日 下午4:36:21
 * @history:
 * @version: v1.0
 */
public class CodeCounter {

	static long normalLines = 0;
	static long commentLines = 0;
	static long whiteLines = 0;

	public static void main(String[] args) {
		File f = new File(
				"D:\\kpchen_workspace\\eclipse_kepler\\web3.0_fileupload\\src\\cn\\test\\utils");
		File[] codeFiles = f.listFiles();
		for (File child : codeFiles) {
			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) {
				line = line.trim();
				if (line.matches("^[\\s&&[^\\n]]*$")) {
					whiteLines++;
				} else if (line.startsWith("/*") && !line.endsWith("*/")) {
					commentLines++;
					comment = true;
				} else if (line.startsWith("/*") && line.endsWith("*/")) {
					commentLines++;
				} else if (true == comment) {
					commentLines++;
					if (line.endsWith("*/")) {
						comment = false;
					}
				} else if (line.startsWith("//")) {
					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();
				}
			}
		}
	}

}


利用正则表达式获取一个网页中所有的邮箱地址

package cn.kpchen.five;

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;

/**
 * 利用正则表达式获取一个网页中所有的邮箱地址
 * 
 * @desc: regulardemo
 * @author: kpchen
 * @createTime: 2014年11月8日 下午4:38:06
 * @history:
 * @version: v1.0
 */
public class EmailSpider {

	public static void main(String[] args) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(
					"C:\\Users\\Administrator\\Desktop\\新建文件夹 (2)\\网易.htm"));
			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());
		}
	}

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间辜负了谁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值