我如何在lua中获取一个文本块并将其合并成一行,类似于html或markdown忽略换行符:
我的标题很容易识别:它们的行从不以字母字符开头。
我已经找出了这个模式:(\n%a.-)\n(%S)。但它并没有合并所有的换行符。(我使用>_<可以很容易地看到合并的行。)
(注:“过程性”有尾随空间。)
>>> t = [[
*lrv-section-1*
1 -- Introduction~
Lua is a powerful, efficient, lightweight, embeddable scripting language.
It supports procedural programming,
object-oriented programming, functional programming,
data-driven programming, and data description.
Lua combines simple procedural
syntax with powerful data description
constructs based on associative arrays and extensible semantics.
Lua is dynamically typed,
runs by interpreting bytecode with a register-based
virtual machine,
and has automatic memory management with
incremental garbage collection,
making it ideal for configuration, scripting,
and rapid prototyping.
]]
>>> print(t:gsub("(\n%a.-)\n(%S)", "%1>_<%2"))
[[
*lrv-section-1*
1 -- Introduction~
Lua is a powerful, efficient, lightweight, embeddable scripting language.>_<It supports procedural programming,
object-oriented programming, functional programming,>_<data-driven programming, and data description.
Lua combines simple procedural >_<syntax with powerful data description
constructs based on associative arrays and extensible semantics.>_<Lua is dynamically typed,
runs by interpreting bytecode with a register-based>_<virtual machine,
and has automatic memory management with>_<incremental garbage collection,
making it ideal for configuration, scripting,>_<and rapid prototyping.
7“构造”和其他许多内容没有合并到前面的行中。
更天真的"(.)\n(%S)"有点工作,但它删除了双行中断,我不知道如何确保我的章节标题保持他们的空格。
>>> print(t:gsub("(.)\n(%S)", "%1>_<%2"))
*lrv-section-1*>_<1 -- Introduction~
>_<Lua is a powerful, efficient, lightweight, embeddable scripting language.>_<It supports procedural programming,>_<object-oriented programming, functional programming,>_<data-driven programming, and data description.
>_<Lua combines simple procedural >_<syntax with powerful data description>_<constructs based on associative arrays and extensible semantics.>_<Lua is dynamically typed,>_<runs by interpreting bytecode with a register-based>_<virtual machine,>_<and has automatic memory management with>_<incremental garbage collection,>_<making it ideal for configuration, scripting,>_<and rapid prototyping.我正在尝试将lua的2html文档处理脚本改编为输出一个vim帮助文件。我计划在合并后使用lume.wordwrap包装行。
发布于 2021-09-09 19:55:38
您可以使用
(%S)[^%S\n]*\n([%a()])此模式匹配:
(%S) -(捕获到组1,%1)任何非空白字符[^%S\n]* -匹配(不捕获)零或多个字符,而不是非空白和换行符(也就是说,它是没有\n的%s模式)。\n -一个新行字符([%a()]) -(捕捉到第2组,%2)任何字母,(或)字符。https://stackoverflow.com/questions/69108487
复制相似问题