LUA: A couple of pattern matching issues
Posted
by Josh
on Stack Overflow
See other posts from Stack Overflow
or by Josh
Published on 2010-04-02T22:19:59Z
Indexed on
2010/04/02
22:23 UTC
Read the original article
Hit count: 442
lua
|pattern-matching
I'm fairly new to lua programming, but I'm also a quick study. 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. I appreciate any help in this matter!
© Stack Overflow or respective owner