2011-03-25 39 views
1

我想移除包含特定Java文檔註釋的註釋正則表達式來移除包含特定單詞的Java註釋塊

例如,我想刪除這個註釋塊

/** 
* @author: Bob 
* @since 28.mar.2008 
* 
*/ 

但不是該塊

/** 
* This class represents ... 
* 
* and so on 
*/ 

到目前爲止,我有這個正則表達式:

^/\*\*(.|\s)+?\*/ 

哪一個註釋塊匹配

但我需要一些條件在那裏(例如塊註釋包含「@since」 我猜猜關鍵是要用前瞻,但我的正則表達式目前並不好。

任何一個誰可以幫助我提高推動這一

感謝 鮑勃

回答

8

由於Java註釋不能嵌套(感謝@聖保羅),對於一個正則表達式。

你可以做到這一點:

^/\*\*(?=(?:(?!\*/)[\s\S])*[email protected]: Bob)(?:(?!\*/)[\s\S])*\*/ 

說明如下:

 
^    # start-of-string 
/\*\*   # literal "/**" (start-of-comment) 

(?=    # begin positive look-ahead (...followed by) 
    (?:   # begin non-capturing group 
    (?!   #  begin negative look-ahead (...not followed by) 
     \*/  #  literal "*/" 
    )   #  end negative look-ahead 
    [\s\S]  #  anything, including newlines 
)*?   # end group, repeat non-greedily 
    @author: Bob # literal "@author: Bob" 
)    # end positive look-ahead 

       # ... now we have made sure there is "@author: Bob" 
       #  before the end of the comment 

(?:    # begin non-capturing group 
    (?!   # begin negative look-ahead 
    \*/   #  literal "*/" 
)    # end negative look-ahead 
    [\s\S]  # anything, including newlines (this eats the comment) 
)*    # end group, repeat 

\*/    # literal "*/" (end-of-comment) 
+0

代碼中的Java文檔註釋並不複雜,而且似乎可行。謝謝 – bob 2011-03-25 14:36:14

+0

非常感謝!它工作得很好。 – bob 2011-03-25 14:52:27

+0

呵呵,從哪裏獲得「Java評論可嵌套」?在'/ *'後註釋轉到下一個'* /',獨立於任何'/ *'之間。 – 2011-03-27 15:56:30

0

你不能簡單地通過javadoc關掉的@author@since的口譯?

-nosince選項可以避免打印@since塊,@author默認不包括(需要-author選項)。

當然,如果你想混淆你沒有自己編寫源代碼,請將其從源代碼中刪除。 (但請確保這是您獲得的代碼的許可範圍內。)

+0

嗨!感謝提示,但這個問題與輸出到JavaDoc無關。問題是源代碼有很多@author的註釋:???我想擺脫它(無需坐幾個小時手動) – bob 2011-04-10 10:04:02

相關問題