正则表达式删除包含特定单词的Java注释块

正则表达式删除包含特定单词的Java注释块

本文介绍了正则表达式删除包含特定单词的Java注释块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除包含特定Java文档注释的注释

I am trying to remove comments containing a specific Java doc comment

例如.我要删除此评论栏

Eg. I want to remove this comment block

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

但不是这个障碍

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

到目前为止,我有这个正则表达式:

So far I have this regexp:

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

与某条注释匹配的

但是我需要在其中的某些条件(例如,包含"@since"的块注释)我想关键是要使用前瞻性,但是我的regex符目前还不够好.

But I need to some conditionals somewhere in there (eg. block comment that contains "@since"I guess the key is to use look aheads, but my regex fu is not that good at the moment.

任何可以帮助我改善这一点的人

Any one who can help me improve this

先谢谢了鲍勃

推荐答案

由于Java注释不能嵌套(感谢@Paŭlo),因此有一个正则表达式.

Since Java comments cannot be nested (thanks @Paŭlo), there is a regex for that.

您可以执行以下操作:

^/\*\*(?=(?:(?!\*/)[\s\S])*?@author: 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)

这篇关于正则表达式删除包含特定单词的Java注释块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:53