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("D://share//courseware//1043633.html")); String line = ""; while((line=br.readLine()) != null) { parse(line); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block 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 reg; import java.util.regex.Matcher; import java.util.regex.Pattern; public class TestExpress { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub p("String的匹配"); //String的匹配"; p("abc".matches("..."));//一个点代表一个字符,匹配 //返回true p("abc".matches("..")); //返回false p("a3345b".replaceAll("//d", "-"));//返回a----b /d代表0-9 p("a3345b".replaceAll("//D", "-"));//返回-3345- /D代表非0-9 p("匹配的方法种类"); //匹配的方法种类 //1.matcher和Pattern long st=System.currentTimeMillis(); Pattern pt=Pattern.compile("[a-z]{3}"); Matcher m=pt.matcher("abc"); p(m.matches()); long ed=System.currentTimeMillis(); //p(ed-st);//速度差不多 //2.上面的话合成一句 p(Pattern.matches("[a-z]{3}", "abc")); //3.String的matches long st1=System.currentTimeMillis(); p("abc".matches("[a-z]{3}")); long ed1=System.currentTimeMillis(); //p(ed1-st1);//速度差不多 p("/********认识.*+?的作用"); /****************认识.*+?的作用***********************/ p("a".matches("."));//true 匹配一个字符 p("aa".matches("aa"));//true 匹配2个字符a p("aaa".matches("a."));//false 如果是"aaa".matches("a..")返回true p("aaaa".matches("a*"));//true 匹配0个或多个 p("aaaa".matches("a+"));//true 至少匹配1个 p("".matches("a*"));//true 匹配0个或多个 p("aaaaaaa".matches("a?"));//false 匹配0个或1个 p("a".matches("a?"));//true 匹配0个或1个 p("".matches("a?"));//true 匹配0个或1个 p("12345678".matches("//d"));//false 匹配1个数字 p("12345678".matches("//d{3,10}"));//true 匹配3-10个数字 p("192.168.1.qqq".matches("//d{1,3}//.//d{1,3}//.//d{1,3}//..{1,3}"));//true p("192".matches("[1-9][1-9][1-9]"));//返回true[]代表范围 p("192".matches("[1-9]{3}"));//返回true p("/********认识范围[]的作用"); /****************认识范围[]的作用***********************/ p("a".matches("[a-z]"));//返回true p("a".matches("[^a-z]"));//返回false p("A".matches("[a-zA-Z]"));//返回true p("A".matches("[a-z]|[A-Z]"));//返回true 效果同上一个 p("A".matches("[a-z|[A-Z]]"));//返回true 效果同上一个 p("R".matches("[A-Z&&[REG]]"));//返回true 在A-Z并且是REG中的一个 p("/********认识预定义的. //d //D //s //S //w //W 的作用"); // /t 制表符 ('/u0009') // /n 新行(换行)符 ('u000A') // /r 回车符 ('u000D') // /f 换页符 ('u000C') // /a 报警 (bell) 符 ('u0007') // /e 转义符 ('u001B') // /cx 对应于 x 的控制符 /****************认识 . //d //D //s //S //w //W 的作用***********************/ p(" ".matches(".")); p("12".matches("//d{2}"));//数字:[0-9] 返回true p("12".matches("//D{2}"));//非数字: [^0-9] 返回false p("/t".matches("//s"));//空白字符:[ /t/n/x0B/f/r] 返回true p("/t".matches("//S"));//非空白字符:[^/s] 返回false p("a_W9".matches("//w{4}"));//单词字符:[a-zA-Z_0-9] 返回true p("a_W9".matches("//W{4}"));//非单词字符:[^/w] 返回false p("abc888&^%".matches("[a-c]{1,3}//d+[&^%#]+"));//综合 返回true //p("//".matches("//"));会报错 //2个饭斜杠代表一个饭斜杠 p("//".matches(""));//这样才返回true p("/n".matches("/n"));//返回true p("//a".matches("//a"));//返回false p("//a".matches("a"));//返回true p("//d".matches("//d"));//返回false p("//d".matches("d"));//返回true p("/********POSIX 字符类(仅 US-ASCII常用于unix"); //POSIX 字符类(仅 US-ASCII) // /p{Lower} 小写字母字符:[a-z] // /p{Upper} 大写字母字符:[A-Z] // /p{ASCII} 所有 ASCII:[/x00-/x7F] // /p{Alpha} 字母字符:[/p{Lower}/p{Upper}] // /p{Digit} 十进制数字:[0-9] // /p{Alnum} 字母数字字符:[/p{Alpha}/p{Digit}] // /p{Punct} 标点符号:!"#$%&'()*+,-./:;<=>?@[/]^_`{|}~ // /p{Graph} 可见字符:[/p{Alnum}/p{Punct}] // /p{Print} 可打印字符:[/p{Graph}/x20] // /p{Blank} 空格或制表符:[ /t] // /p{Cntrl} 控制字符:[/x00-/x1F/x7F] // /p{XDigit} 十六进制数字:[0-9a-fA-F] // /p{Space} 空白字符:[ /t/n/x0B/f/r] p("a".matches("//p{Lower}"));//true p("Z".matches("//p{Lower}"));//false p("/********边界匹配器 "); // 边界匹配器 // ^ 行的开头 // $ 行的结尾 // /b 单词边界 // /B 非单词边界 // /A 输入的开头 // /G 上一个匹配的结尾 什么意思? // /Z 输入的结尾,仅用于最后的结束符(如果有的话) 什么意思? 换行、回车、下一行、行分隔符、段落分隔符 // /z 输入的结尾 p("hello sir".matches("^h.*"));//true p("hello sir".matches("h.*$"));//true p("hello sir".matches("h.*ir$"));//true p("hello sir".matches("^h[a-z]{1,3}o//b.*"));//true p("hellosir".matches("^h[a-z]{1,3}o//b.*"));//false //例如/b 匹配一个单词边界,也就是指单词和空格间的位置。例如, //'er/b' 可以匹配"never" 中的 'er',但不能匹配 "verb" 中 //的 'er'。 ///B 匹配非单词边界。'er/B' 能匹配 "verb" 中的 'er',但不能匹 //配 "never" 中的 'er'。 p("hellosir".matches("//Ah.*"));//true p("hellosir".matches("//Ae.*"));//false //p("hellr sir".matches("h.*r//G")); p("hellosir".matches("h.*r//z"));//true p("hellosir".matches(".*r//Z"));//true p("hellosir/n".matches(".*r/n//Z"));//true //匹配空白行 p("/********匹配空白行 "); p("/t/n".matches("^[//s&&[^//n]]*//n$"));//true p("/t/n".matches("^[//s&&[^//n]]*$"));//true p("/n".matches("//s"));//true 换行符 p("/r".matches("//s"));//true 回车符 p("/f".matches("//s"));//true 换页符 p("/t".matches("//s"));//true 制表符 //换行符就是另起一行,回车符就是回到一行的开头, //所以我们平时编写文件的回车符应该确切来说叫做回车换行符 p("/********匹配email "); //email p("wxg6203@sina.com".matches("[//w[.-]]+@[//w[.-]]+//.[//w]+")); p("/********找子串和lookingAt "); Pattern p=Pattern.compile("//d{3,5}"); Matcher mc=p.matcher("123-4567-890-12"); p(mc.matches());//false mc.reset();//重置,吃进去的吐出来 p(mc.find());//true匹配一次,去掉一次 p(mc.start()+"-"+mc.end());//0-3 p(mc.find());//true匹配剩下的 p(mc.start()+"-"+mc.end());//4-8 p(mc.find());//true p(mc.start()+"-"+mc.end());//9-12 p(mc.find());//false //p(mc.start()+"-"+mc.end());找不到会报错 //每次都是从头找 p(mc.lookingAt());//true p(mc.lookingAt());//true p(mc.lookingAt());//true p(mc.lookingAt());//true //字符串的替换 p("/********字符串的替换 "); //Pattern.CASE_INSENSITIVE 启用不区分大小写的匹配。 Pattern pa=Pattern.compile("java",Pattern.CASE_INSENSITIVE); Matcher me=pa.matcher("java Java i love java you hate java tails"); //p(me.replaceAll("JAVA"));//输出JAVA JAVA i love JAVA you hate JAVA //将奇数的java替换成大写,偶数的java替换成小写 StringBuffer sb=new StringBuffer(); int i=0; while(me.find()){ i++; if(i%2==0){ me.appendReplacement(sb, "java"); }else{ me.appendReplacement(sb, "JAVA"); } } me.appendTail(sb);//没有这句会加不上tails p(sb.toString()); p("/********分组 "); //分组 Pattern pp=Pattern.compile("//d{3,5}[a-z]{2}"); String s="123aa-32443bb-234cc-00"; Matcher mr=pp.matcher(s); while(mr.find()){ p(mr.group()); //123aa //32443bb //234cc } //改一下 pp=Pattern.compile("(//d{3,5})([a-z]{2})"); mr=pp.matcher(s); while(mr.find()){ p(mr.group(1)); //123 //32443 //234 } p("/********贪婪的***********Greedy "); Pattern pp1=Pattern.compile("(.{3,10})[0-9]"); s="aaaa5bbbb6"; Matcher m1=pp1.matcher(s); p(m1.matches());//true m1.reset(); if(m1.find()){ p(m1.start()+"-"+m1.end());//0-10 }else{ p("not match"); } p("/********不情愿的***********Reluctant "); Pattern pp2=Pattern.compile("(.{3,10}?)[0-9]"); s="aaaa5bbbb6"; Matcher m2=pp2.matcher(s); p(m2.matches());//true m2.reset(); if(m2.find()){ p(m2.start()+"-"+m2.end());//0-5 }else{ p("not match"); } p("/***********独占性的***********Possessive "); Pattern pp3=Pattern.compile("(.{3,10}+)[0-9]"); s="aaaa5bbbb6";//一下子吞进10个,不吐出啦 Matcher m3=pp3.matcher(s); p(m3.matches());//false m3.reset(); if(m3.find()){ p(m3.start()+"-"+m3.end()); }else{ p("not match"); } p("/***********特殊构造(非捕获)lookahead*********** "); String s1="4444a66bc"; Pattern pp4=Pattern.compile(".{3}(?=a)");//以a结尾但是不包括a Matcher m4=pp4.matcher(s1); while(m4.find()){ p("(?=a)放在后"+m4.group());//444 } pp4=Pattern.compile("(?=a).{3}");//以a开头但是不包括a m4=pp4.matcher(s1); while(m4.find()){ p("(?=a)放在前"+m4.group());//a66 } pp4=Pattern.compile(".{3}(?!a)"); m4=pp4.matcher(s1); while(m4.find()){ p("(?!a)放在后"+m4.group());//4a6 } pp4=Pattern.compile("(?!a).{3}"); m4=pp4.matcher(s1); while(m4.find()){ p("(?!a)放在前"+m4.group());//4a6 } pp4=Pattern.compile("(?:a).{3}"); m4=pp4.matcher(s1); while(m4.find()){ p("(?:a)放在前"+m4.group());//a66b } pp4=Pattern.compile(".{3}(?:a)"); m4=pp4.matcher(s1); while(m4.find()){ p("(?:a)放在后"+m4.group());//444a } //其他看api p("/***********向前引用*********** "); //匹配组:主要看左括号出现的编号 Pattern pp5=Pattern.compile("(//d//d)//1"); s1="1212"; Matcher m5=pp5.matcher(s1); p(m5.matches());//返回true p(m5.groupCount());//1个组 ,是12,所以最后两位12要与这个组的字符相同,才匹配 pp5=Pattern.compile("((//d)(//d))//3"); s1="122"; m5=pp5.matcher(s1); p(m5.matches());//返回false p(m5.groupCount());//3个组 分别是12,1,2.其中s1最后一个2要与第三组的数相同,才匹配。 pp5=Pattern.compile("(//d(//d))//2"); s1="122"; m5=pp5.matcher(s1); p(m5.matches());//返回true p(m5.groupCount());//2个组 //忽略大小写,下面2句等同 //pp5=Pattern.compile("java",Pattern.CASE_INSENSITIVE); //p("Java".matches("(?i)(java)"));//true } public static void p(Object o){ System.out.println(o); } }
下面是测试目录下java 代码的行号
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) { File f = new File("D://share//JavaProjects//TankWar1.9.11//src"); 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(); } } } } }
用正则表达式提取邮箱
