【问题标题】:Pattern match a string in Lua模式匹配 Lua 中的字符串
【发布时间】:2011-03-29 14:21:11
【问题描述】:

我有以下字符串要使用 Lua 拆分成一个表: (数据是相互对齐的。我没有找到如何在这个网站上写成这样的格式)

IP:192.168.128.12
MAC: AF:3G:9F:c9:32:2E
过期:2010 年 8 月 13 日星期五 20:04:53
剩余时间:11040 秒

结果应该像这样放在一个表格中:

t = {“IP”:“192.168.128.12”,“MAC”:“AF:3G:9F:c9:32:2E”,“过期”:“2010 年 8 月 13 日星期五 20:04:53”, “剩余时间”:“11040 秒”}

我试过了:

for k,v in string.gmatch(data, "([%w]+):([%w%p%s]+\n") do
  t[k] = v
end

这是我最好的尝试。

【问题讨论】:

    标签: lua lua-patterns


    【解决方案1】:

    如果我理解了您的用例,以下应该可以解决问题。不过,它可能需要一些调整。

    local s = "IP: 192.168.128.12 MAC: AF:3G:9F:c9:32:2E Expires: Fri Aug 13 20:04:53 2010 Time Left: 11040 seconds"
    local result = {}
    result["IP"] = s:match("IP: (%d+.%d+.%d+.%d+)")
    result["MAC"] = s:match("MAC: (%w+:%w+:%w+:%w+:%w+:%w+)")
    result["Expires"] = s:match("Expires: (%w+ %w+ %d+ %d+:%d+:%d+ %d+)")
    result["Time Left"] = s:match("Time Left: (%d+ %w+)")
    

    【讨论】:

      【解决方案2】:

      假设“数据相互对齐”的含义如下:

      IP:192.168.128.12 MAC:AF:3G:9F:c9:32:2E 过期:2010 年 8 月 13 日星期五 20:04:53 剩余时间:11040 秒

      <pre> 标签可用于保持对齐。

      尽量减少对现有代码的更改:

      for k,v in string.gmatch(data, "(%w[%w ]*):%s*([%w%p ]+)\n") do t[k] = v end
      
      • 将第一次捕获更改为(%w[%w ]*),以避免前导空格并在Time Left 中获得空间
      • : 之后添加%s*,以避免在捕获的值中出现前导空格
      • 在第二次捕获时将%s 更改为空格,以避免捕获\n
      • 修复了 gmathgmatch 的拼写错误,并添加了 ) 以进行捕获

      【讨论】:

        【解决方案3】:

        下面的模式应该适合你,前提是:

        1. IP 地址是十进制点分符号。
        2. MAC 地址是用冒号分隔的十六进制。

        注意:您问题中提供的 MAC 地址有一个不是十六进制数字的“G”。

        编辑:在详细考虑了您的问题后,我扩展了我的答案,以展示如何将多个实例捕获到一个表中。

        sString = [[
        IP: 192.168.128.16
        MAC: AF:3F:9F:c9:32:2E
        Expires: Fri Aug 1 20:04:53 2010
        Time Left: 11040 seconds
        
        IP: 192.168.128.124
        MAC: 1F:3F:9F:c9:32:2E
        Expires: Fri Aug 3 02:04:53 2010
        Time Left: 1140 seconds
        
        IP: 192.168.128.12
        MAC: 6F:3F:9F:c9:32:2E
        Expires: Fri Aug 15 18:04:53 2010
        Time Left: 110 seconds
        ]]
        
        local tMatches = {}
        
        for sIP, sMac, sDate, sSec in sString:gmatch("IP:%s([%d+\.]+)%sMAC:%s([%x+:]+)%sExpires:%s(.-)%sTime%sLeft:%s(%d+)%s%w+") do
            if sIP and sMac and sDate and sSec then
                print("Matched!\n"
                        .."IP: "..sIP.."\n"
                        .."MAC: "..sMac.."\n"
                        .."Date: "..sDate.."\n"
                        .."Time: "..sSec.."\n")
        
                table.insert(tMatches, { ["IP"]=sIP, ["MAC"]=sMac, ["Date"]=sDate, ["Expires"]=sSec })
            end
        end
        
        print("Total Matches: "..table.maxn(tMatches))
        
        for k,v in ipairs(tMatches) do
            print("IP Address: "..v["IP"])
        end
        

        【讨论】:

          猜你喜欢
          • 2021-06-07
          • 1970-01-01
          • 2018-03-11
          • 2013-10-13
          • 2011-06-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-06
          相关资源
          最近更新 更多