簡體   English   中英

僅從每個Java文件的頂部刪除多行注釋

[英]Remove Multiline Comments only from Top of Every Java File

我們曾經使用borland starteam工具(一種類似Mercurial的修訂/源代碼控制系統之一)進行代碼管理。 每當我們提交代碼時,工具本身都會在文件頂部放置對提交的描述。 所以現在我們在每個文件頂部的代碼中都有許多類。 例如:

/*This is some developer comment at the top of the file*/

/*
 * $Log:
 *  1   Client Name 1.0   07/11/2012 16:28:54  Umair Khalid did something
 *  2   Client Name 1.0   07/11/2012 16:28:54  Umair Khalid again did 
 *                                             something
 * $
 */

public class ABC
{
  /*This is just a variable*/
  int a = 0;
  public int method1()
  {
  }
}

現在,我計划刪除每個文件頂部顯示的所有starteam類型代碼。 但是我不想從任何文件或頂部的任何其他版權評論中刪除任何其他評論。 我只想刪除以$ Log開始並以$結尾的塊。 我已經看過其他與此問題相關的問題,但這是多行注釋。 正則表達式將是一個很好的選擇嗎?

有什么我可以使用的實用程序,而不是編寫我自己的代碼來刪除的?

如果正則表達式是唯一的快速解決方案,那么我就會陷入困境。

任何幫助,將不勝感激。

如果格式完全符合您的顯示格式,則可以構建一個像這樣的易碎小狀態機。

從枚舉開始跟蹤狀態:

enum ParseState
{
    Normal,
    MayBeInMultiLineComment,    //occurs after initial /*
    InMultilineComment,
}

然后添加以下代碼:

     public static void CommentStripper()
     {
         var text = @"/*This is some developer comment at the top of the file*/
/*
 * $Log:
 *  1   Client Name 1.0   07/11/2012 16:28:54  Umair Khalid did something
 *  2   Client Name 1.0   07/11/2012 16:28:54  Umair Khalid again did 
 *                                             something
 * $
 */

/*
    This is not a log entry
*/

public class ABC
{
  /*This is just a variable*/
  int a = 0;
  public int method1()
  {
  }
}";

    //this next line could be File.ReadAllLines to get the text from a file
    //or you could read from a stream, line by line.

    var lines = text.Split(new[] {"\r\n"}, StringSplitOptions.None);

    var buffer = new StringBuilder();
    ParseState parseState = ParseState.Normal;
    string lastLine = string.Empty;

    foreach (var line in lines)
    {
        if (parseState == ParseState.Normal)
        {
            if (line == "/*")
            {
                lastLine = line;
                parseState = ParseState.MayBeInMultiLineComment;
            }
            else
            {
                buffer.AppendLine(line);
            }
        }
        else if (parseState == ParseState.MayBeInMultiLineComment)
        {
            if (line == " * $Log:")
            {
                parseState = ParseState.InMultilineComment;
            }
            else
            {
                parseState = ParseState.Normal;
                buffer.AppendLine(lastLine);
                buffer.AppendLine(line);
            }
            lastLine = string.Empty;
        }
        else if (parseState == ParseState.InMultilineComment)
        {
            if (line == " */")
            {
                parseState = ParseState.Normal;
            }
        }

    }
    //you could do what you want with the string, I'm just going to write it out to the debugger console.
    Debug.Write(buffer.ToString());
}

請注意,使用lastLine是因為您需要lastLine讀一行以選擇注釋是否為日志條目( MayBeInMultiLineComment狀態跟蹤的內容)。

該輸出看起來像:

/*This is some developer comment at the top of the file*/


/*
    This is not a log entry
*/

public class ABC
{
  /*This is just a variable*/
  int a = 0;
  public int method1()
  {
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM