Lua 中模式匹配的几个模式匹配问题

发布于 2024-08-27 04:41:27 字数 2013 浏览 5 评论 0原文

我一直在为我使用的程序开发天气预报器,并且它在很大程度上运行良好。这是我到目前为止所拥有的。 (不要注意 zs.stuff。这是特定于程序的,与 Lua 编码无关。)

if not http then http = require("socket.http") end  

local locale = string.gsub(zs.params(1),"%s+","%%20")
local page = http.request("http://www.wunderground.com/cgi-bin/findweather/getForecast?query=" .. locale .. "&wuSelect=WEATHER")
local location = string.match(page,'title="([%w%s,]+) RSS"')
--print("Gathering weather information for " .. location .. ".")
--local windspeed = string.match(page,'<span class="nobr"><span class="b">([%d.]+)</span>&nbsp;mph</span>')
--print(windspeed)
local condition = string.match(page, '<td class="vaM taC"><img src="http://icons-ecast.wxug.com/i/c/a/[%w_]+.gif" width="42" height="42" alt="[%w%s]+" class="condIcon" />')
--local image = string.match(page, '<img src="http://icons-ecast.wxug.com/i/c/a/(.+).gif" width="42" height="42" alt="[%w%s]+" class="condIcon" />')
local temperature = string.match(page,'pwsvariable="tempf" english="&deg;F" metric="&deg;C" value="([%d.]+)">')
local humidity = string.match(page,'pwsvariable="humidity" english="" metric="" value="(%d+)"')
zs.say(location)
--zs.say("image ./Images/" .. image .. ".gif")
zs.say("<color limegreen>Condition:</color> <color white>" .. condition .. "</color>")
zs.say("<color limegreen>Temperature: </color><color white>" .. temperature .. "F</color>")
zs.say("<color limegreen>Humidity: </color><color white>" .. humidity .. "%</color>")

我的主要问题是:我更改了“条件”并将“图像”变量添加到了现在的状态。尽管它应该匹配的行直接来自网页,但它根本无法匹配。所以我想知道我错过了什么阻止了这段代码的工作。如果我拿出
< img src="http://icons-ecast.wxug.com/i/c/a/[%w_]+.gif"
它会完美地匹配条件。 (无论出于何种原因,我无法让上面的行正确显示,但是 `< 和 img 之间没有空格)

任何人都可以指出它有什么问题吗?除了模式匹配之外,我向您保证该行是从网页中逐字逐句提取的。

我遇到的另一个问题是跨行匹配的能力。有没有可能的方法来做到这一点?我问的原因是因为在同一页面上,我需要匹配的一些内容被分解在不同的行上,并且由于我想要匹配的实际模式显示在页面上的其他位置,我需要能够跨换行符匹配以获得准确的模式。

I've been working on a weather forecaster for a program that I use, and it's working well, for the most part. Here is what I have so far. (Pay no attention to the zs.stuff. That is program specific and has no bearing on the Lua coding.)

if not http then http = require("socket.http") end  

local locale = string.gsub(zs.params(1),"%s+","%%20")
local page = http.request("http://www.wunderground.com/cgi-bin/findweather/getForecast?query=" .. locale .. "&wuSelect=WEATHER")
local location = string.match(page,'title="([%w%s,]+) RSS"')
--print("Gathering weather information for " .. location .. ".")
--local windspeed = string.match(page,'<span class="nobr"><span class="b">([%d.]+)</span> mph</span>')
--print(windspeed)
local condition = string.match(page, '<td class="vaM taC"><img src="http://icons-ecast.wxug.com/i/c/a/[%w_]+.gif" width="42" height="42" alt="[%w%s]+" class="condIcon" />')
--local image = string.match(page, '<img src="http://icons-ecast.wxug.com/i/c/a/(.+).gif" width="42" height="42" alt="[%w%s]+" class="condIcon" />')
local temperature = string.match(page,'pwsvariable="tempf" english="°F" metric="°C" value="([%d.]+)">')
local humidity = string.match(page,'pwsvariable="humidity" english="" metric="" value="(%d+)"')
zs.say(location)
--zs.say("image ./Images/" .. image .. ".gif")
zs.say("<color limegreen>Condition:</color> <color white>" .. condition .. "</color>")
zs.say("<color limegreen>Temperature: </color><color white>" .. temperature .. "F</color>")
zs.say("<color limegreen>Humidity: </color><color white>" .. humidity .. "%</color>")

My main issue is this: I changed the 'condition' and added the 'image' variables to what they are now. Even though the line it's supposed to be matching comes directly from the webpage, it fails to match at all. So I'm wondering what it is I'm missing that's preventing this code from working. If I take out the
<td class="vaM taC">< img src="http://icons-ecast.wxug.com/i/c/a/[%w_]+.gif"
it'll match condition flawlessly. (For whatever reason, I can't get the above line to display correctly, but there is no space between the `< and img)

Can anyone point out what is wrong with it? Aside from the pattern matching, I assure you the line is verbatim from the webpage.

Another question I had is the ability to match across line breaks. Is there any possible way to do this? The reason why I ask is because on that same page, a few of the things I need to match are broken up on separate lines, and since the actual pattern I'm wanting to match shows up in other places on the page, I need to be able to match across line breaks to get the exact pattern.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾城如夢未必闌珊 2024-09-03 04:41:27

您可以大大简化您的比赛(见下文),但总的来说,您似乎遇到了两个问题...

  • 缺少您想要捕获的比赛周围的 ()。
  • 你需要逃离。匹配中的字符,将其设置为 %。

我尝试了这个,它工作了......

local page = [[<td class="vaM taC"><img src="http://icons-ecast.wxug.com/i/c/a/hello_world.gif" width="42" height="42" alt="HELLO WOLRD" class="condIcon" />]]
local condition, image = string.match(page, '.+/([%w_]+)%.gif".+alt="([%w%s]+)".+')
print(condition, image)

这个打印......

hello_world    HELLO WORLD

至于多行,这不应该是一个问题,换行符只是控制字符,如果您将多行读入同一个字符串,则此匹配有效。

You can simplify your match considerably (see below) but in general it looks like you've got two problems...

  • Missing the () around the match you wish to capture.
  • You need to escape the . characters in your match by making them %.

I tried this and it worked...

local page = [[<td class="vaM taC"><img src="http://icons-ecast.wxug.com/i/c/a/hello_world.gif" width="42" height="42" alt="HELLO WOLRD" class="condIcon" />]]
local condition, image = string.match(page, '.+/([%w_]+)%.gif".+alt="([%w%s]+)".+')
print(condition, image)

this printed...

hello_world    HELLO WORLD

as for multiline, that should not be a problem, the newlines are just control characters and if you read in multiple lines into the same string this match works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文