在LUA中,我需要从具有特定内容的一行读取文本文件到具有特定内容的另一行。我该怎么办?
这是一个例子
名为aaa.txt的文本文件
...
...
...
[Main from here on]
line1
line2
line3
...
...
title=Till here
...
...
因此,我需要计算方格线之间的线,并从该线开始[从此处开始的主要标题](标题如此),直到最后一行称为“ title = Till here”
该解决方案基于io.lines ()
迭代器:
--[[
This function will return all lines from <file>
from <from> till <to> or end of file
both ends included.
--]]
local function readFromTo (file, from, to)
io.input (file) -- open file.
local started = false
local lines = {}
for line in io.lines () do
if not started and line == from then
started = true
end
if started then
lines [#lines + 1] = line
if line == to then
-- <to> found:
return lines
end
end
end
-- Only if <to> not found:
return lines
end
print (table.concat (readFromTo ('aaa.txt', '[Main from here on]', 'title=Till here'), '\n'))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句