在Lua中获取两个特定行之间的行

使用 io.open 读取文件,现在我想获取在另外两行之间的特定行。

文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<somethings>
    <something attribute="some" />
    <something attribute="some1" />
</somethings>

我想要获取存储在表中的 <somethings></somethings> 之间的行。我目前的代码:

local file = io.open(file, "r")
local arr = {}

for line in file:lines() do
    table.insert(arr, line);
end

但是它会将所有行插入到数组中。

点赞
用户107090
用户107090

尝试这个:

local collecting=false
for line in file:lines() do
    if line:match("</somethings>") then
       collecting=false  -- 或者如果只有一个块则中断
    end
    if collecting then
       table.insert(arr, line)
    end
    if line:match("<somethings>") then
       collecting=true
    end
end
2015-12-14 15:17:47