我已经工作一天气预报员一个程序,我使用,以及它的工作好吧,对于大部分。这里是什么我有这么远。(不注意到zs。东西。这是程序的具体和无关的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>")

我的主要问题是这个:我改变的'条件',增添'图像'的变量,为什么他们现在的样子。即使该行它应该是匹配的是直接从网页,它匹配失败。所以我想知道它是什么我失,是防止这种代码的工作。如果我取出
<td class="vaM taC">< img src="http://icons-ecast.wxug.com/i/c/a/[%w_]+.gif"
它会比赛的条件完美无缺。(无论出于何种原因,我不能上线正确显示,但是没有空间`< 和img)

谁能指出什么是错呢?除了模式匹配的,我向你保证,该线是逐字从网页。

另一个问题我曾经拥有的能力相匹配的跨线断裂。是否有任何可能的方式做到这一点?为什么我问是因为在同一页上,一些事,我需要的比赛被打破了在单独行,并且由于实际的模式,我想要匹配表示了在其他地方上的页面,我必须要能够比赛跨过线断得到确切的模式。

有帮助吗?

解决方案

你可以简化你的比赛很大(见下文),但一般看起来你已经有了两个问题...

  • 失踪的()周围的比赛你想要捕获。
  • 你需要逃避的。字在你的匹配,使它们%.

我尝试这样做,它的工作...

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

作为对多,这不应该是一个问题,新行的只是控制人物,如果你读了在多行为相同的串这个匹配的工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top