正则表达式的简单应用:
利用正则表达式统计代码中的(代码行数,注释行数,空白行数)。利用正则表达式获取一个网页中所有的邮箱地址
利用正则表达式统计代码中的(代码行数,注释行数,空白行数)@H_404_8@
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();
}
}
}
}
}
@H_404_8@
利用正则表达式获取一个网页中所有的邮箱地址@H_404_8@
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());
}
}
}
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(); } } } } }
@H_404_8@