【问题标题】:How to Find string inside html tags in lua如何在lua中的html标签中查找字符串
【发布时间】:2016-07-24 10:04:54
【问题描述】:

使用 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

如何在&lt;span class&gt; 中找到字符串。

谢谢,

【问题讨论】:

  • 您的代码充满了语法错误," 字符包含在以" 分隔的字符串和未完成的以' 分隔的字符串中。修复语法错误后问题是否仍然存在?
  • 需要注意的是,语法错误非常明显,连 Stack Overflow 的语法高亮都注意到了。
  • @hugomg,我在我的代码中没有看到任何语法错误,你看到任何错误吗?如果是这样,请告诉我那些是什么?
  • 如果您尝试运行您在此处发布的代码,Lua 会指出存在语法错误的行。基本上,当你想在字符串中包含引号字符时,你需要小心。
  • 您运行的代码显然与您在 SO 上发布的代码不同。

标签: lua


【解决方案1】:

您需要确保find 中的pater 不会得到

... 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

【讨论】:

  • 非常感谢 Jakuje.. 它工作正常。本地 x, y = string.find(mystring,'', 0, true) 本地 w, z = string.find(mystring,'', 0, true),我在这一行中使用了 zero(0),我明白我必须使用什么。再次感谢...
  • 我看到您浏览了Tour 页面,所以我相信您知道如何处理解决您问题的答案。提示:左边有“嘀”声。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
相关资源
最近更新 更多