【问题标题】:Regex copy first value in line正则表达式复制行中的第一个值
【发布时间】:2014-05-03 07:30:58
【问题描述】:

不确定即使使用正则表达式/Sublime Text 2 是否可行。希望获取一行的第一个(正)值(1759)并将其以注释格式复制到每个相关行的末尾。复制到所有(负值“B”行,直到下一个/新的正行,其中该正值(1753)被复制到相关行。我有超过 1600 行具有这种格式的评论,想知道是否有一种比手动输入更快的方法。注意“B”行与“C”行无关。

s(1759,"B 1",{99168,99157,99158,99159,105779,105792},4) 
s(-1764,"B 2 (L)",{105819,105140,104956,105141,105086,105119},nil) 
s(-1763,"B 3 (R)",{99086,99080,99081,99082,105086,105119},4) 
s(-1762,"B 4 (L)",{105432,105638,105454,105639,105584,105617},nil) 
s(-1761,"B 5 (R)",{99406,99402,99403,99404,105584,105617},4) 
s(-1760,"B 6 (L)",{105183,105809,105754,105800,105779,105792},nil)
s(1753,"C",{105161,99108,99109,99104,105784,105790},64)
s(-1755,"C (R)",{105410,99352,99353,99354,105597,105598},64)
s(-1754,"C (L)",{105329,105161,105259,105404,105755,105784,105790},nil)

进入:

s(1759,"B 1",{99168,99157,99158,99159,105779,105792},4) --1759 
s(-1764,"B 2 (L)",{105819,105140,104956,105141,105086,105119},) --1759 
s(-1763,"B 3 (R)",{99086,99080,99081,99082,105086,105119},4) --1759 
s(-1762,"B 4 (L)",{105432,105638,105454,105639,105584,105617},) --1759 
s(-1761,"B 5 (R)",{99406,99402,99403,99404,105584,105617},4) --1759 
s(-1760,"B 6 (L)",{105183,105809,105754,105800,105779,105792},) --1759 
s(1753,"C",{105161,99108,99109,99104,105784,105790},64) -- 1753
s(-1755,"C (R)",{105410,99352,99353,99354,105597,105598},64) -- 1753
s(-1754,"C (L)",{105329,105161,105259,105404,105755,105784,105790},nil) -- 1753

【问题讨论】:

  • 哪些行被认为是“相关的”?
  • 不是真正的 Lua 问题。它更像是一个正则表达式/追加。
  • 所有这些行都通过“B”关联,如果您愿意,第一行下面的所有行都共享名称“B”。但是在实际代码中,我有许多不同/不相关的块。例如编辑代码

标签: regex append sublimetext2


【解决方案1】:

这是我能用 ST 实现的最接近您想要做的事情。
查找内容:s\((?<!\-)(\d+)(.+\n)(?:((?:s\((?=\-).+\d+\n)+)|(s\((?=\-).+))+
替换为:s($1$2$3$4 --$1

您必须多次运行它,因为它一次只能运行一次。

【讨论】:

  • 这很好用,它确实注释掉了一些行 "--###s(####" 需要一个正则表达式来 f&r 所有 "--###s(" 和 "s( “我准备好了!谢谢!
  • 这将删除行首的 cmets,后跟 "s(": ^--.+(?=s\() 无需在替换字段中放置任何内容。此外,如果您可以单击此答案旁边的复选标记它帮助了你。
【解决方案2】:

我认为,仅使用正则表达式替换无法完成此任务。

我没有使用Sublime 文本编辑器,因此无法提供可用于此文本编辑器的解决方案。

但是,也许还有其他人有类似的任务,因此为文本编辑器 UltraEdit 提供脚本解决方案。

以下 UltraEdit 脚本将您的输入数据重新格式化为输出数据。

if (UltraEdit.document.length > 0)  // Is any file opened?
{
   // Define environment for this script.
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();

   // Check if last line of file has a line termination and if this
   // is not the case, append a line termination to end of the file.
   UltraEdit.activeDocument.bottom();
   if (UltraEdit.activeDocument.isColNumGt(1))
   {
      UltraEdit.activeDocument.insertLine();
   }
   // Move caret to top of the active file.
   UltraEdit.activeDocument.top();

   // Select Perl regular expression engine.
   UltraEdit.perlReOn();

   // Define once all find and replace parameters
   // not changed during execution of the script.
   UltraEdit.activeDocument.findReplace.matchCase=true;
   UltraEdit.activeDocument.findReplace.matchWord=false;
   UltraEdit.activeDocument.findReplace.regExp=true;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
   {
      UltraEdit.activeDocument.findReplace.searchInColumn=false;
   }
   UltraEdit.activeDocument.findReplace.preserveCase=false;
   UltraEdit.activeDocument.findReplace.replaceAll=true;
   UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;

   UltraEdit.activeDocument.findReplace.mode=0;   // Find in current file.

   // Run the following loop until no line is found anymore up to
   // end of the file starting with "s(" and a positive number.
   while(UltraEdit.activeDocument.findReplace.find("^s\\(\\d+"))
   {
      // Copy just the found number into a string variable
      var sNumber = UltraEdit.activeDocument.selection.substr(2);
      // Remember the number of the current line.
      var nBlockBegin = UltraEdit.activeDocument.currentLineNum;
      // Run the same find as before to find next line starting with "s("
      // and a positive number to determine the lines of the block to modify.
      if(!UltraEdit.activeDocument.findReplace.find("^s\\(\\d+"))
      {
         UltraEdit.activeDocument.bottom();
      }
      var nNextBlock = UltraEdit.activeDocument.currentLineNum;

      // Select from current position of caret to beginning of the block.
      UltraEdit.activeDocument.gotoLineSelect(nBlockBegin,1);
      UltraEdit.activeDocument.endSelect();

      // The following Replace All is done only on selected lines.
      UltraEdit.activeDocument.findReplace.mode=1;

      // On all selected lines ending with "nil)" or just with ")" and
      // optional spaces/tabs remove "nil" and append " --" and the number.
      UltraEdit.activeDocument.findReplace.replace("(?:nil)?\\)[ \\t]*$", ") --"+sNumber);

      // Switch back to a find in current file.
      UltraEdit.activeDocument.findReplace.mode=0;

      // Move caret to beginning of next block or end of file.
      UltraEdit.activeDocument.gotoLine(nNextBlock,1);
   }
   UltraEdit.activeDocument.top();
}

我使用 UltraEdit v21.10.0.1027 在输入数据示例中测试了这个脚本。

PS:您的输出数据有点不一致。在以1759 结尾的行上,-- 和数字之间没有空格,而在以1753 结尾的行上,-- 之后有一个空格。并且在 3 行上删除了 nil,但没有在输出数据示例的最后一行。

【讨论】:

    【解决方案3】:

    从源文件生成 file1.txt(插入所有 1600 行):

    file1.txt

    local comment, group
    print((([[
    s(1759,"B 1",{99168,99157,99158,99159,105779,105792},4)
    s(-1764,"B 2 (L)",{105819,105140,104956,105141,105086,105119},nil)
    s(-1763,"B 3 (R)",{99086,99080,99081,99082,105086,105119},4)
    s(-1762,"B 4 (L)",{105432,105638,105454,105639,105584,105617},nil)
    s(-1761,"B 5 (R)",{99406,99402,99403,99404,105584,105617},4)
    s(-1760,"B 6 (L)",{105183,105809,105754,105800,105779,105792},nil)
    s(1753,"C",{105161,99108,99109,99104,105784,105790},64)
    s(-1755,"C (R)",{105410,99352,99353,99354,105597,105598},64)
    s(-1754,"C (L)",{105329,105161,105259,105404,105755,105784,105790},nil)
    ]]):gsub('(s%((.-),"(%w+).-)\n',
       function(line, next_comment, next_group)
          if group ~= next_group then
             group = next_group
             comment = next_comment
          end
          return line..' -- '..comment..'\n'
       end
    )))
    

    并在 Lua 中执行

    lua file1.txt > file2.txt
    

    之后file2.txt 将包含修改后的代码

    【讨论】:

    • 谢谢!我应该在代码中包含全名。一些 s( 被复制到包含相同开头名称值的其他行:s(2025,"B 1 (P)",{1435...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多