3

私が使用しているプログラムの天気予報に取り組んできましたが、ほとんどの場合、うまく機能しています。これが私がこれまでに持っているものです。(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>")

私の主な問題は次のとおりです。「条件」を変更し、「画像」変数を現在のものに追加しました。一致するはずの行が Web ページから直接来ているにもかかわらず、まったく一致しません。だから、このコードが機能しないのは何が欠けているのだろうと思っています。
<td class="vaM taC">< img src="http://icons-ecast.wxug.com/i/c/a/[%w_]+.gif"を取り出すと、
完全に条件に一致します。(何らかの理由で、上記の行を正しく表示することはできませんが、`< と img の間にスペースはありません)

誰がそれの何が悪いのか指摘できますか? パターン マッチングは別として、この行は Web ページからそのまま引用したものであることを保証します。

私が持っていた別の質問は、改行をまたいで一致させる機能です。これを行う方法はありますか?私が質問する理由は、同じページで、照合する必要があるもののいくつかが別の行に分割されており、照合したい実際のパターンがページの他の場所に表示されるためです。正確なパターンを取得するために改行全体で一致できるようにします。

4

1 に答える 1

1

マッチをかなり単純化できますが (以下を参照)、一般的には 2 つの問題があるように見えます...

  • キャプチャしたい試合の周りに () がありません。
  • をエスケープする必要があります。それらを%にすることで、あなたの試合の文字。

私はこれを試してみましたが、うまくいきました...

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

複数行に関しては問題ありません。改行は単なる制御文字であり、複数行を同じ文字列に読み込むと、この一致が機能します。

于 2010-04-02T22:46:02.523 に答える