查找一片文章中的所有邮箱地址:
public class EmailSpiler {
public static void main(String[] args) {
try {
BufferedReader buf = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\dfsafd.html"));
String line ="";
while((line=buf.readLine())!=null){
parse(line);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 邮箱的正则
* @param line
*/
private static void parse(String line) {
Pattern p = Pattern.compile("(\\w)+(\\.\\w+)*@((\\w)+(\\.\\w{2,3}){1,3})");
Matcher m = p.matcher(line);
while(m.find()){
System.out.println(m.group());
}
}
}
这篇是看你写的代码有多少注释 多少空行 以及实际代码量
public class CodeCounter {
//正常
static long normalLine=0;
//注释行
static long commentLine =0;
//空行
static long whiteLine=0;
public static void main(String[] args) {
File f = new File("F:\\新建文件夹 (2)\\shiro02\\src\\main\\java\\com\\wskj\\app\\util\\");
File[] codeFiles = f.listFiles();
for (File child : codeFiles) {
if (child.getName().matches(".*\\.java$")) {
parse(child);
}
}
System.out.println("正常代码行数:normalLine="+normalLine);
System.out.println("注释行数:commentLine="+commentLine);
System.out.println("空白行数:whiteLine="+whiteLine);
}
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 = replaceBlank(line);
if(line.matches("^[\\s&&[^\\n]]*$")){
whiteLine ++;
}else if(line.startsWith("/*") && line.endsWith("*/")){
commentLine++;
}else if(line.startsWith("/*") && !line.endsWith("*/")){
commentLine++;
comment = true;
}else if(true == comment){
commentLine++;
if(line.endsWith("*/")){
comment=false;
}
}else if(line.startsWith("//")){
commentLine++;
}else{
normalLine++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 替换所有的空格 制表 换行 为 ""
* @param str
* @return
*/
public static String replaceBlank(String str) {
String dest = "";
if (str!=null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
return dest;
}
}