我想匹配这种情况的非注释的代码:
/***
注释部分**/
test word我想取得test word,而过滤掉注释里面的内容,注释有几种,以下几种全部过滤,不要注释里面的东西:1. // /  // comment
2./** comment **/
3./**
comment**/这个正则表达式应该怎样写?? 多谢!

解决方案 »

  1.   


    getLine.startsWith("//")
    getLine.startsWith("/*") && getLine.endsWith("*/")
      

  2.   


    String src = "//ab\r\n cccc\r\n /*ddd*/ eee";
    String reg = "(/\\*.*\\*/)|(//[^\\r\\n]*)";Pattern p = Pattern.compile(reg, Pattern.DOTALL); //DOTALL不可省略Matcher m = p.matcher(src);
    String result = m.replaceAll("");
      

  3.   


    String src = "000//ab\r\n cccc\r\n fff /*ddd*/ eee"; //测试用的源文本
    String reg = "(/\\*.*\\*/)|(//[^\\r\\n]*)"; //正则表达式Pattern p = Pattern.compile(reg, Pattern.DOTALL); //DOTALL不可省略
    Matcher m = p.matcher(src);String result = m.replaceAll(""); //获得非注释的文本
      

  4.   

    public static void main(String[] args) {
     String str = "/***"
      +"\n 注释部分"
      +"\n"
      +"awsad"
      +"\n**/" +
      "asdfasdfsdf";
     Pattern p = Pattern.compile("(/\\*\\*\\*[\\s|\\S]*\\*\\*/)", Pattern.MULTILINE);
     Matcher m = p.matcher(str);
     while(m.find())
     System.out.println(m.group(1));
    }