即使使用正则表达式/ 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
答案 0 :(得分:2)
这是我能用ST做的最接近你想做的事
找到什么:s\((?<!\-)(\d+)(.+\n)(?:((?:s\((?=\-).+\d+\n)+)|(s\((?=\-).+))+
替换为:s($1$2$3$4 --$1
你必须多次运行它,因为它一次只能运行一次。
答案 1 :(得分:0)
我认为,使用正则表达式替换无法完成此任务。
我没有使用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
被删除,但不在输出数据示例的最后一行。
答案 2 :(得分:0)
从源文件创建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
将包含修改后的代码