How to Find string inside html tags in lua

时间:2016-04-04 18:11:47

标签: lua

Using lua script I want to find sting inside string. This is how I am trying but not working.

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

How do I find string inside <span class>.

Thanks,

1 个答案:

答案 0 :(得分:0)

You need to make sure your pater in find will not get

... considered "magic" ...

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

Running example