public class Demo12 { public static void main(String[] args) { BufferedReader br = null; try { br = new BufferedReader(new FileReader("C:\\emailTest.html")); String line = ""; while((line = br.readLine()) != null){//读取文件的每一行 parse(line);//解析其中的email地址 } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(br != null){ try { br.close(); br = null; } 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()); } } } //输出结果 2819531636@qq.com 2819531636@qq.com 2405059759@qq.com 2405059759@qq.com 1013376804@qq.com ...
实战2: 代码统计小程序•最后的一个实战案例: 统计一个项目中一共有多少行代码, 多少行注释, 多少个空白行. 不妨对自己做过的项目进行统计, 发现不知不觉中也是个写过成千上万行代码的人了...
•我在github上挑选了一个项目, 是纯java写的小项目, 方便统计. 点击跳转
•下面是具体的代码, 除了判断空行用了正则表达式外, 判断代码行和注释行用了String类的api
public class Demo13 { private static long codeLines = 0; private static long commentLines = 0; private static long whiteLines = 0; private static String filePath = "C:\\TankOnline"; public static void main(String[] args) { process(filePath); System.out.println("codeLines : " + codeLines); System.out.println("commentLines : " + commentLines); System.out.println("whiteLines : " + whiteLines); } /** * 递归查找文件 * @param pathStr */ public static void process(String pathStr){ File file = new File(pathStr); if(file.isDirectory()){//是文件夹则递归查找 File[] fileList = file.listFiles(); for(File f : fileList){ String fPath = f.getAbsolutePath(); process(fPath); } }else if(file.isFile()){//是文件则判断是否是.java文件 if(file.getName().matches(".*\\.java$")){ parse(file); } } } private static void parse(File file) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); String line = ""; while((line = br.readLine()) != null){ line = line.trim();//清空每行首尾的空格 if(line.matches("^[\\s&&[^\\n]]*$")){//注意不是以\n结尾, 因为在br.readLine()会去掉\n whiteLines++; }else if(line.startsWith("/*") || line.startsWith("*") || line.endsWith("*/")){ commentLines++; }else if(line.startsWith("//") || line.contains("//")){ commentLines++; }else{ if(line.startsWith("import") || line.startsWith("package")){//导包不算 continue; } codeLines++; } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(null != br){ try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } } } } //输出结果 codeLines : 1139 commentLines : 124 whiteLines : 172
贪婪模式与非贪婪模式•经过两个实战后, 相信大家已经掌握了正则表达式的基本使用了, 下面介绍贪婪模式与非贪婪模式. 通过查看官方api我们发现Pattern类中有如下定义:
Greedy quantifiers 贪婪模式X? X, once or not at all
X* X, zero or more times
X+ X, one or more times
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n but not more than m times
X?? X, once or not at all
X*? X, zero or more times
X+? X, one or more times
X{n}? X, exactly n times
X{n,}? X, at least n times
X{n,m}? X, at least n but not more than m times
X?+ X, once or not at all
X*+ X, zero or more times
X++ X, one or more times
X{n}+ X, exactly n times
X{n,}+ X, at least n times
X{n,m}+ X, at least n but not more than m times
•这三种模式表达的意思是一样的, 在前面的讲解中我们全部使用的是贪婪模式. 那么其他两种模式的写法有什么区别呢? 通过下面的代码示例进行讲解.
public static void main(String[] args) { Pattern p = Pattern.compile(".{3,10}[0-9]"); String s = "aaaa5bbbb6";//10个字符 Matcher m = p.matcher(s); if(m.find()){ System.out.println(m.start() + " - " + m.end()); }else { System.out.println("not match!"); } } //输出结果 0 - 10