【问题标题】:Lua string.match pattern for MSN weatherserviceMSN 天气服务的 Lua string.match 模式
【发布时间】:2013-12-10 11:26:39
【问题描述】:

我使用MSN weatherservice。现在我对string.match 有以下问题。除sWindRichtung 外,所有变量均已填充。它等于nil

sHumidity, rest = string.match(rest,"humidity=\"([^\"]+)\"(.*)");
sWind, rest = string.match(rest,"windspeed=\"([^\"]+)\"(.*)");
sWindRichtung, rest = string.match(rest,"winddisplay=\"([^\"]+)\"(.*)");

要过滤的字符串是:humidity="77" winddisplay="11 km/uur N" windspeed="11"

我认为字符/ 是问题所在。

【问题讨论】:

  • 问题是你的解析假设了一个特定的字段顺序。解析 windspeed 之后没有任何内容,因此 rest 是一个空字符串。
  • 感谢您的回答。我找到了解决方案。
  • 添加您的解决方案作为答案。

标签: string lua lua-patterns


【解决方案1】:

您可以一次性解析字符串。试试这个:

s = [[
humidity="77" winddisplay="11 km/uur N" windspeed="11"
]]

for k,v in s:gmatch('(%a+)="(.-)"') do
        print(k,v)
end

当然,您可能希望将值保存在表格中。

【讨论】:

    猜你喜欢
    • 2018-02-03
    • 2015-02-08
    • 1970-01-01
    • 2012-08-21
    • 2011-11-05
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    相关资源
    最近更新 更多