23---java正则表达式02(应用)

 

1、抓取文件里的所有email地址:
 import java.util.regex.Pattern; 
 import java.util.regex.Matcher;
 import java.io.*;
 public class Demo{
  public static void main(String args[]) throws Exception{ 
    BufferedReader br=new BufferedReader(new FileReader("D:\\java.txt"));
    String line=null;
    while((line=br.readLine())!=null){
      parse(line);
     }
   }
  public static void parse(String line){
    //注意如果点(.)没在中括号里面,必须进行转义
    Pattern p=Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");
    Matcher m=p.matcher(line);
    while(m.find()){//查找匹配符
     //m.group()将查到到的字符串返回
     System.out.println(m.group());
     }
   }
  }

2、统计代码:
 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:\\java");
   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();
     }
    }
   }
  }

 }

3、

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Test {

 public static void main(String[] args) {
  //简单认识正则表达式的概念
  /*
  p("abc".matches("..."));
  p("a8729a".replaceAll("
\\d", "-"));
  Pattern p = Pattern.compile("[a-z]{3}");
  Matcher m = p.matcher("fgh");
  p(m.matches());
  p("fgha".matches("[a-z]{3}"));
  */
  
  //初步认识. * + ?
  /*
  p("a".matches("."));
  p("aa".matches("aa"));
  p("aaaa".matches("a*"));
  p("aaaa".matches("a+"));
  p("".matches("a*"));
  p("aaaa".matches("a?"));
  p("".matches("a?"));
  p("a".matches("a?"));
  p("214523145234532".matches("
\\d{3,100}"));
  p("192.168.0.aaa".matches("
\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));
  p("192".matches("[0-2][0-9][0-9]"));
  */
  
  //范围
  /*
  p("a".matches("[abc]"));
  p("a".matches("[^abc]"));
  p("A".matches("[a-zA-Z]"));
  p("A".matches("[a-z]|[A-Z]"));
  p("A".matches("[a-z[A-Z]]"));
  p("R".matches("[A-Z&&[RFG]]"));
  */
  
  //认识\s \w \d \
  /*
  p(" \n\r\t".matches("
\\s{4}"));
  p(" ".matches("
\\S"));
  p("a_8".matches("
\\w{3}"));
  p("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+"));
  p("
\\".matches("\\\\"));
  */
  
  //POSIX Style
  //p("a".matches("
\\p{Lower}"));
  
  //boundary
  /*
  p("hello sir".matches("^h.*"));
  p("hello sir".matches(".*ir$"));
  p("hello sir".matches("^h[a-z]{1,3}o\\b.*"));
  p("hellosir".matches("^h[a-z]{1,3}o\\b.*"));
  //whilte lines
  p(" \n".matches("^[\\s&&[^\\n]]*\\n$"));
    
  p("aaa 8888c".matches(".*\\d{4}."));
  p("aaa 8888c".matches(".*\\b\\d{4}."));
  p("aaa8888c".matches(".*\\d{4}."));
  p("aaa8888c".matches(".*\\b\\d{4}."));
  */
  
  //email
  //p("
asdfasdfsafsf@dsdfsdf.com".matches("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+"));
  
  //matches find lookingAt
  /*
  Pattern p = Pattern.compile("
\\d{3,5}");
  String s = "123-34345-234-00";
  Matcher m = p.matcher(s);
  p(m.matches());
  m.reset();
  p(m.find());
  p(m.start() + "-" + m.end());
  p(m.find());
  p(m.start() + "-" + m.end());
  p(m.find());
  p(m.start() + "-" + m.end());
  p(m.find());
  //p(m.start() + "-" + m.end());
  p(m.lookingAt());
  p(m.lookingAt());
  p(m.lookingAt());
  p(m.lookingAt());
  */
  
  //replacement
  /*
  Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
  Matcher m = p.matcher("java Java JAVa JaVa IloveJAVA you hateJava afasdfasdf");
  StringBuffer buf = new StringBuffer();
  int i=0;
  while(m.find()) {
   i++;
   if(i%2 == 0) {
    m.appendReplacement(buf, "java");
   } else {
    m.appendReplacement(buf, "JAVA");
   }
  }
  m.appendTail(buf);
  p(buf);
  */
  
  //group
  /*
  Pattern p = Pattern.compile("(
\\d{3,5})([a-z]{2})");
  String s = "123aa-34345bb-234cc-00";
  Matcher m = p.matcher(s);
  while(m.find()) {
   p(m.group());
  }
  */
  
  //qulifiers
  /*
  Pattern p = Pattern.compile(".{3,10}+[0-9]");
  String s = "aaaa5bbbb68";
  Matcher m = p.matcher(s);
  if(m.find())
   p(m.start() + "-" + m.end());
  else
   p("not match!");
  */
  
  //non-capturing groups
  /*
  Pattern p = Pattern.compile(".{3}(?=a)");
  String s = "444a66b";
  Matcher m = p.matcher(s);
  while(m.find()) {
   p(m.group());
  }
  */
  
  //back refenrences
  /*
  Pattern p = Pattern.compile("(
\\d(\\d))\\2");
  String s = "122";
  Matcher m = p.matcher(s);
  p(m.matches());
  */
  
  //flags的简写
  //Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
  p("Java".matches("(?i)(java)"));
 }
 
 public static void p(Object o) {
  System.out.println(o);
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

bzuld

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

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

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

打赏作者

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

抵扣说明:

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

余额充值