怎么用正则表达式,比如str="hafdsdgdfgfdgg;
/*fdadasfsd
fdasfdsf*/
fasfjsdf;
fsadf;
/*fsadf*/
    
fdasfgs;
/*fdasfsd
fdasf*/
fdasfds;";我想要用程序删除注释里内容包括/**/,谁能给点代码看看

解决方案 »

  1.   

    for example
    String s = "hafdsdgdfgfdgg;\n/*fdadasfsd\nfdasfdsf*/\nfasfjsdf;"; //so on
    String result = s.replaceAll("[/][*](.*?\\s+)*(.*?|\\s+)[*][/]", "");
    System.out.println(result);
      

  2.   

    比如一个程序里所有注释,包括两种//,
                  //注释东西
    String str2 = "";
    int end = 0;
                 void main();//注释怎么用
      

  3.   

    str==
    package test;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    public class CPTest1 {



    public static void main(String[] args) throws IOException {

    FileInputStream fs = new FileInputStream("1.in");
    int start = str.indexOf("


    byte[] buffer = new byte[3];

    int length;

    /*while(-1 != (length = fs.read(buffer, 0, 3)))
    {
    String str = new String(buffer,0, length);*/

    System.out.println(str);
    }

    fs.close();
    /*DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(
    new FileOutputStream("1.txt")));
    byte b = 3;
    int i = 12;
    char ch = 'a';
    float f = 3.3f;
    dos.writeByte(b);
    dos.writeInt(i);
    dos.writeChar(ch);
    dos.writeFloat(f);
    dos.close();*/
    DataInputStream dis = new DataInputStream(new BufferedInputStream(
    new FileInputStream("1.in")));


      }
    }
    str是以上字符串,可是用String str2 = str.replaceAll("[/][*](.*?\\s+)*(.*?|\\s+)[*][/]", "");
    输不出来。是怎么回事
      

  4.   

    一行一行,什么意思?
    你把所有的内容都读入到一个字符串里再替换应该可以
    如果是一行一行读入,那么要自己进行判断
    for example
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("CPTest1.java")));
    String buf; 
    StringBuilder content = new StringBuilder(); 
    while((buf=br.readLine())!=null) { 
        content.append(buf).append("\n"); //把文件所有的内容保存到一个content中
    }
    br.close();
    String text = content.toString();
    text = text.replaceAll("[/][*](.*?\\s*)*(.*?|\\s*)[*][/]", ""); //替换/**/之间的内容
    text = text.replaceAll("//.*?(\n)+", "\n"); //替换//的注释,如果删除后面的空白行,则(\n)+,否则\n就可以了
    System.out.println(text);如果需要一行一行读入处理,就自己判断/*从哪里开始,然后判断*/在哪里结束,然后这部分的内容不保存到content中,//的处理相同
      

  5.   

    text = text.replaceAll("//.*?(\n)+", "\n"); //替换//的注释,如果删除后面的空白行,则(\n)+,否则\n就可以了。
    这句我运行,怎么删不了呢
      

  6.   

    不知道你整体是如何处理的
    可能你的//注释行的最后没有带\n换行符,改成
    text = text.replaceAll("//.*?(\n+|.*)", "\n");