java String以及正则(2)

来源:互联网 发布:淘宝扫码让别人登录 编辑:程序博客网 时间:2024/05/24 01:08

正则表达式的替换:replaceAll与replaceFirst只能匹配全部或第一个,不能执行渐进的替换

代码:   String s ="java Java JAVA jaVA  llOVEjava asd";
               Pattern p = Pattern.compile("java",Pattern.CASE_INSENSITIVE);

              Matcher m =p.matcher(s);

              System.out.println( m.replaceAll("java"))

//对正则进行编译,然后m就可以进行替换,它将扫描输入序列以查找该模式的匹配项(字串)。

eg2:要求给一段代码,把偶数小写,奇数大写

               String s ="java Java JAVA jaVA llOVEjava asd";

                Pattern p = Pattern.compile("java",Pattern.CASE_INSENSITIVE);

               Matcher m =p.matcher(s);
               StringBuffer buf = new StringBuffer();
                int i =0;
               while(m.find()){
                       i++;
                      if(i%2==0){
                      m.appendReplacement(buf, "java");//读到第几个就把符合的代码换成"java"
                    }else{
                      m.appendReplacement(buf, "JAVA");
                    }
          }
           m.appendTail(buf);
           System.out.println(buf);

appendReplacement:1:当m.find()时候,找到匹配的把替换。其他的字符不变,同样放在buf里面,如果找到的是最后一个,后面的都是尾巴,可以理解m知道,通过appendTail,就可以加到buf里面。

正则的分组:Pattern p= Pattern.compile("(\\d{3,5})([a-z]){2}");

Pattern

  while(m.find(){

m.group();

System.out.println(m.group(1));

})

m.group();//返回匹配的大组

m.group(1);//返回匹配的大组

eg3:      String str ="adsa?123ads";
           Pattern p1 = Pattern.compile("(\\d+)(\\w+)");
           Matcher m1 =p1.matcher(str);
           while(m1.find()){
          System.out.println(m1.group());

                   System.out.println(m1.group(1))

结果:123ads

123

经典例子:来自马士兵的java教程

网上扒邮箱:

public class ParseEmail {
    public static void parse(String t){
    Pattern p =Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w[.-]]+");
    Matcher m = p.matcher(t);
    while(m.find()){
    System.out.println(m.group());
    }
    }
public static void main(String[] args) {
      try {
BufferedReader b = new BufferedReader(new FileReader("D:/workspace/String/email.txt"));
String s ="";
while((s = b.readLine())!= null){
parse(s);
}
} catch (FileNotFoundException e) {
 System.out.println("没有文件");
  }catch(IOException e){
  e.printStackTrace();
  }
}

}

统计代码:

public class CodeCount {
   private static int whiteLine;
   private static int commentLine;
   private static int normalLine;
public static void main(String[] args) {
       File f = new File("D:/workspace/Interface/");
       File[] listFile = f.listFiles();
       for(File f1 : listFile){
          if(f1.getName().matches(".*\\.java$")){
           count(f1);
          }
       }
       System.out.println(whiteLine);
       System.out.println(commentLine);
       System.out.println(normalLine);


}
public static void count(File s){
BufferedReader br = null;
boolean comment = false;
try {
    br = new BufferedReader(new FileReader(s));
    String line ="";
    while((line = br.readLine())!=null){
    //在这里需要注意原本空行的正则还有结尾的\n,但是现在readLine方法把它删去了
                 if(line.matches("^[\\s&&[^\\n]]*$")){
                whiteLine++;
                 }else if(line.trim().startsWith("/*")&& line.endsWith("*/")){
                     commentLine++;
                 }else if(line.trim().startsWith("/*")&& !line.endsWith("*/")){
                //这里需要注意空格
                commentLine++;
                comment = true;
                 }else if(true ==comment){
                commentLine++;
                if(line.endsWith("*/")){
                        comment = false;
                }
                 }else if(line.trim().startsWith("//")){
                         commentLine++;
                 }else{
                    normalLine++;
                 }
    }
} catch (FileNotFoundException e) {
   System.out.println("没有文件");
 }catch(IOException e){
   e.printStackTrace();
 }finally{
 if(br!=null){
 try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
 }
 }
}
}

俩个例子很好结合IO,正则

后续。。。。。。。。。

原创粉丝点击