Lua函数在SciTE中输出匹配的字符串

时间:2012-02-19 05:24:31

标签: lua scintilla scite

我知道如何通过简单地使用editor:MarkerNext()来输出匹配字符串的行(来自find命令的结果):

function print_marked_lines()

    local ml = 0
    local lines = {}

    while true do
        ml = editor:MarkerNext(ml, 2)
        if (ml == -1) then break end
        table.insert(lines, (editor:GetLine(ml)))
        ml = ml + 1
    end

    local text = table.concat(lines)
    print(text)

end

我不知道的是如何仅输出匹配的字符串(不像发布的片段那样输出整行)。我假设有解决方案,因为匹配的字符串被突出显示,并且必须具有一些允许提取它们的属性,但我想需要Scintilla知识,因为我在提供的SciTE绑定中找不到任何引用。

查找/匹配所有正则表达式模式“I \ w +”的示例屏幕截图:

enter image description here

我想输出(打印到输出窗格)所有突出显示的字符串部分

1 个答案:

答案 0 :(得分:1)

@theta,令人讨厌的问题(至少这对我而言):)

问题是在Scite GUI“查找/替换”对话框中,您使用一个正则表达式语法来匹配模式,并使用反斜杠(例如,\s);在Scite lua函数中,您对模式使用不同的语法,使用百分号(相应地,%s) - 请参阅我在Lua pattern matching vs. regular expressions - Stack Overflow中的帖子。从那里,你有这两个参考:

相应地,您的功能代码(“输出(打印到输出窗格)所有突出显示的字符串部分”)将是:

function print_marked_lines()

  local sel = editor:GetSelText()

  for mymatch in sel:gmatch"I %w+" do -- note; a regex match!
    print(mymatch)
  end

end

在示例文本的输出窗格中输出:

I don
I assume
I guess
I couldn

希望这有帮助,
干杯!