Click to See Complete Forum and Search --> : Regex to Match Javadoc Comments


alexin
December 26th, 2008, 09:34 AM
I'm naively using the following regular expression to match Javadoc comments:

Regular Expression

/\*(.*)\*/


Input Example

/**
* Javadoc comment
*/


Unfortunately, the expression doesn't match, probably due to the multiline input sequence - it works when the input is on a single line. Shoudn't the dot metacharacter match \r and \n?

I have used similar expressions, for other purposes, that work fine with multiple lines.


I'm clueless since several days. Any suggestion is welcome! Thanks.

keang
December 26th, 2008, 10:59 AM
First of all the example you have shown isn't a Java Doc, it's a multi-line comment. To be a Java Doc it needs to start with a forward slash and 2 asterisks ie /**

If you read the API docs for the Pattern class it says:The regular expression . matches any character except a line terminator unless the DOTALL flag is specified.It also mentions multi-line support using the MULTILINE flag. Are you using these flags when you compile the expression?

alexin
December 26th, 2008, 11:17 AM
First of all the example you have shown isn't a Java Doc, it's a multi-line comment. To be a Java Doc it needs to start with a forward slash and 2 asterisks ie /**

Oops, that's a typo. I'll fix it.

Your suggestion solved the problem. I missed the DOTALL information flag when reading about the MULTILINE just below, in the docs. In fact, all my searches were related to multiple lines.

Thank you for your time.

keang
December 26th, 2008, 11:26 AM
My pleasure, I'm glad it solved your problem.