views:

60

answers:

1

I want to get a regex which will match a tag in a java comment so I can replace it with a .net comment.

eg I have this:

/**
* Some method description
* 
* @param paramName Parameter description
*         which may span more than 1 line
* @return return value.
* @throws ExceptionName some exception description
*           again may span more than 1 line.
*/

and I want to end up with this:

///
/// Some method description
/// 
/// <param name="paramName"> Parameter description
///         which may span more than 1 line</param>
/// <returns> return value.</returns>
/// <exception cref="ExceptionName"> some exception description
///           again may span more than 1 line.</exception>
///

The part that I'm not sure about is the best way to deal with matching the @tag and potentially the name afterwards and how to match the inner text that will go between the angle brackets.

Any ideas appreciated.

+1  A: 

A single regex is not the best way to go at all here, since you are not doing the same thing with @param and @throws vs. @return. Scripting this in Perl or Python would make this task far easier.

That being said, these should get you on your way ... (All these are in Python's syntax) this:

(?i)(?s)(/\*\*.*?\*/)

will get the entire comment. This:

^(/\*\*|\*/|\*)(.*)$

replaced with ///\2 will turn all of your comment stars with .Net comments.

(?i)(?s)(^([^@]+)@param (\b.*\b)(.*?)(?!\* @return)

replaced with

(\1<param name="\2">\3</param>)

should fix the param portion. Likewise,

(?i)(?s)(^([^@]+)@throws (\b.*\b)(.*?)(?!\*/)

replaced with

(\1<exception cref="\2">\3</param>)

should fix the throws portion. Finally,

@return (.*)

replaced with \1 should fix the @return. (Note this one is not set as multi line [The star does not match EOL characters])

This would all need to be plugged into a script (or else run, one after the other in your editor of choice).

Please note that these do not attempt to account for any stray stars in your @pram, @return, @throw portions.

Sean Vieira
thanks. Yeah I didn't intend to use a single regex, but will be performing a series of replaces. This will help me get started. Much appreciated. Now if only it was in .Net syntax...:)
Sam Holder
I'm afraid that I don't know .NET's regular expression syntax off the top of my head. A quick perusal of http://www.regular-expressions.info/dotnet.html suggests that you will want to change all of my backreferences (\1 for example) to $1 and use .NET's regular expression class flags (RegexOptions.IgnoreCase for (?i) and RegexOptions.Singleline for (?s) ). Otherwise, it all *should* work. (Test in http://regexhero.net/tester/ perhaps)
Sean Vieira