如何在lua中查找html标签内的字符串

使用lua脚本,我想在字符串中查找字符串。 这是我尝试的方法,但不起作用。

local mystring="<span class="my-author"><a href="http://aaaa/author/abb-abbb/" title="Posts by abb" class="author" rel="author">abc xyz</a></span>"

local x, y= string.find(mystring,'<span class=\"my-author"\>')
local w, z = string.find(mystring,'</a></span>)
local author
if y==nil or w==nil then
    print(author)
else
    author = string.sub(mystring, y+1, w-1)
            print(author)
end

我如何在<span class>中查找字符串。

谢谢,

点赞
用户2196426
用户2196426

你需要确保在 findpatter 中不会被认为是 “魔法”,即不会被 Lua 认为是特殊字符。

local mystring='<span class="my-author"><a href="http://aaaa/author/abb-abbb/" title="Posts by abb" class="author" rel="author">abc xyz</a></span>'

local x, y = string.find(mystring,'<span class="my-author">', 0, true)
local w, z = string.find(mystring,'</a></span>', x, true)
local author
print(x,y,w,z)
if y==nil or w==nil then
    print(author)
else
    author = string.sub(mystring, y+1, w-1)
            print(author)
end

示例运行

2016-04-04 18:38:17