A couple of issues with pattern matching with pattern matching in Lua

I am working on a weather predictor for the program I use, and it works well, for the most part. Here is what I still have. (Ignore zs.stuff. This is program specific and has nothing to do with 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>&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>")

: "" "image" , . , , , -, . , , .   <td class="vaM taC"> < img src= "http://icons-ecast.wxug.com/i/c/a/[%w_]+.gif"
. ( - , `< img

- , ? , , -.

, , - . ? , , , , , , , , , , .

+3
1

(. ), , ...

  • () , .
  • . , %.

, ...

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

, , - , , .

+1

Source: https://habr.com/ru/post/1739652/


All Articles