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

发布于 2025-01-07 08:07:30 字数 667 浏览 2 评论 0原文

我知道如何通过简单地使用 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+”的屏幕截图示例:

在此处输入图像描述

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

I know how to output lines of matched strings (result from find command) by simply using editor:MarkerNext():

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

What I don't know is how to output only matched strings (not whole line as with posted snippet). I assume there is solution as matched strings are highlighted and must have some property which would allow extracting them, but I guess Scintilla knowledge is needed as I couldn't find any reference in provided SciTE bindings.

Example screenshot for find/match all regex pattern "I \w+":

enter image description here

I want to output (print to output pane) all highlighted string parts

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2025-01-14 08:07:30

@theta,令人讨厌的问题(至少对我来说):)

问题是在 Scite GUI“查找/替换”对话框中,您使用一种正则表达式语法来匹配模式,并带有反斜杠(例如, \s);而在 Scite lua 函数中,您对模式使用不同的语法,带有百分号(相应地,%s) - 请参阅我在 Lua 模式匹配与正则表达式 - 堆栈内存溢出从那里,您将获得以下两个参考资料:

  • SciTE 正则表达式 - 用于 Scite 的正则表达式模式语法GUI“查找/替换”对话框
    • 注意,这里他们使用 \l 作为实际字符类的替代(如 \w 等)!李>
  • < a href="http://lua-users.org/wiki/PatternsTutorial" rel="nofollow noreferrer">lua-users wiki:模式教程 - 在 lua Scite 中的扩展脚本

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

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

希望这有帮助,
干杯!

@theta, nasty question (at least it was for me) :)

The problem is that in the Scite GUI "find/replace" dialog, you use one regex syntax for match patterns, with backslash (say, \s); while in Scite lua functions, you use a different syntax for patterns, with percent sign (correspondingly, %s) - see my posting in Lua pattern matching vs. regular expressions - Stack Overflow. From there, you have these two references:

Correspondingly, code for your function ("to output (print to output pane) all highlighted string parts") would be:

function print_marked_lines()

  local sel = editor:GetSelText()

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

end

Outputs this in output pane from your example text:

I don
I assume
I guess
I couldn

Hope this helps,
Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文