mercredi 5 août 2015

How to write a java program to filter all commented lines and print only java coding lines?


I tried using regular expression to filter the single and multi-line comments from my text file. I am able to filter all the comments like //it works

/*
* welcome
*/
/* hello*/

but I am not able to remove the following comment

/*
sample
*/

This is my code:

import java.io.*;
import java.lang.*;


class TestProg
{
public static void main(String[] args) throws IOException {
    removeComment();
}
static void removeComment() throws IOException
{
    try {
        BufferedReader br = new BufferedReader(new FileReader("d:\\data.txt"));
        String line;
        while((line = br.readLine()) != null){
            if(line.contains("/*") && line.contains("*/") || line.contains("//")) {

                System.out.println(line.replaceAll("(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|(?://.*)","")); 
            }
            else if(line.contains("/*") || line.contains("*") || line.contains("*/")) {

                continue;
            }
            else
                System.out.println(line); 
        }
        br.close();
    }

    catch(IOException e) {
        System.out.println("OOPS! File could not read!");
    }
}
}

Please help me to solve this...

Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire