Groovy or java: how to get a block of comments using regex from / ** *** /?

advertisements

This might be a piece of cake for java experts. Please help me out:

I have a block of comments in my program like this:

/*********
block of comments - line 1
line 2
.....
***/

How could I retrieve "block of comments" using regex?

thanks.


Something like this should do:

    String str =
        "some text\n"+
        "/*********\n" +
        "block of comments - line 1\n" +
        "line 2\n"+
        "....\n" +
        "***/\n" +
        "some more text";

    Pattern p = Pattern.compile("/\\*+(.*?)\\*+/", Pattern.DOTALL);
    Matcher m = p.matcher(str);

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

(DOTALL says that the . in the pattern should also match new-line characters) Prints:

block of comments - line 1
line 2
....