Lua函数在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+”的屏幕截图示例:
我想要输出(打印到输出窗格)所有突出显示的字符串部分
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+":
I want to output (print to output pane) all highlighted string parts
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@theta,令人讨厌的问题(至少对我来说)
:)
问题是在 Scite GUI“查找/替换”对话框中,您使用一种正则表达式语法来匹配模式,并带有反斜杠(例如,
\s
);而在 Scitelua
函数中,您对模式使用不同的语法,带有百分号(相应地,%s
) - 请参阅我在 Lua 模式匹配与正则表达式 - 堆栈内存溢出从那里,您将获得以下两个参考资料:\l
作为实际字符类的替代(如\w
等)!李>lua
Scite 中的扩展脚本%l
类确实存在,表示小写字符(除了%w
等)!相应地,您的函数的代码(“输出(打印到输出窗格)所有突出显示的字符串部分”)将是:
从示例文本在输出窗格中输出此内容:
希望这有帮助,
干杯!
@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 Scitelua
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:\l
as a stand-in for actual character classes (like\w
and others)!lua
extension scripts in Scite%l
class actually exists, meaning lowercase characters (in addition to%w
and others)!Correspondingly, code for your function ("to output (print to output pane) all highlighted string parts") would be:
Outputs this in output pane from your example text:
Hope this helps,
Cheers!