javacode-analysisreadlineblock-comments

Check if the line contains /*


I want to check if my line contains /* or not. I know how to check if the block comment is in the start:

/* comment starts from the beginning and ends at the end */

if(line.startsWith("/*") && line.endsWith("*/")){

      System.out.println("comment : "+line);  
}

What I want to know is how to figure the comment is, like this:

something here /* comment*/

or

something here /*comment */ something here

Solution

  • Try using this pattern :

    String data = "this is amazing /* comment */ more data ";
        Pattern pattern = Pattern.compile("/\\*.*?\\*/");
    
        Matcher matcher = pattern.matcher(data);
        while (matcher.find()) {
            // Indicates match is found. Do further processing
            System.out.println(matcher.group());
        }