我尝试解析文本,并根据标记执行操作。
案文是:
<window>
<caption>My window
</window>
<panel>
<label>
<caption>
<position>50,50
<color>255,255,255
</label>
</panel>
代码:
function parse_tag(chunck)
for start_tag,tag_name in string.gfind(chunck,"(<(.-)>)") do
if (child_obj[tag_name]) then
print(start_tag)
for data,end_tag in string.gfind(chunck,"<" .. tag_name ..">(.-)(</" .. tag_name ..">)") do
for object_prop,value in string.gfind(data,"<(.-)>(.-)") do
print("setting property = \"" .. object_prop .. "\", value of" .. value);
end
end
print("</" .. tag_name ..">");
elseif(findInArray(main_obj,tag_name)) then
print("Invalid data");
stop();
end
end
end
for key,tag in ipairs(main_obj) do
for start_tag,tag_name,chunck,end_tag in string.gfind(data,"(<(" .. tag.name .. ")>)(.-)(</" .. tag.name .. ">)") do --> searching for window/panel start and end tags
if (findInArray(main_obj,tag_name)) then
print(start_tag)
parse_tag(chunck); --> parses the tag with child tag
print(end_tag)
end
end
end
似乎无法获取值,因为我得到以下输出:
<window>
</window>
<panel>
<label>
setting property = "caption", value of
setting property = "position", value of
setting property = "color", value of
</label>
</panel>
如何在第一个<%tag%>
之后使用匹配字符串,直到下一个<%tag%>
或结尾。
答案 0 :(得分:1)
string.gfind(data,"<(.-)>(.-)")
在这里,您尝试将值与.-
匹配。但是,-
是懒惰的,即.-
会尝试尽可能少地匹配,在这种情况下,是一个空字符串。
尝试告诉它匹配到下一个<
:
string.gfind(data,"<(.-)>(._)<")
答案 1 :(得分:0)
尝试了不同类型的捕获。 此
string.gfind(data,"<(.-)>([^%<+.-%>+]+)")
似乎工作