Lua 中的模式匹配字符串

发布于 2024-09-14 18:05:50 字数 508 浏览 5 评论 0原文

我使用 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

这是我最好的尝试。

I have the following string to split into a table using Lua:
(the data is aligned with each other. I did not find out how to write format it like that on this site)

IP: 192.168.128.12
MAC: AF:3G:9F:c9:32:2E
Expires: Fri Aug 13 20:04:53 2010
Time Left: 11040 seconds

the result should be put into a table like this:

t = {"IP" : "192.168.128.12", "MAC" : "AF:3G:9F:c9:32:2E", "Expires" : "Fri Aug 13 20:04:53 2010", "Time Left" : "11040 seconds"}

I tried with:

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

That was my best attempt.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟雨凡馨 2024-09-21 18:05:50

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

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+)")

If I understood your use case the following should do the trick. It might require a bit of tweaking though.

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+)")
维持三分热 2024-09-21 18:05:50

假设“数据彼此对齐”意味着如下所示:

IP:          192.168.128.12
MAC:         AF:3G:9F:c9:32:2E
Expires:     Fri Aug 13 20:04:53 2010
Time Left:   11040 seconds

 标记可用于保持对齐。

最大限度地减少对现有代码的更改:

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

Assuming "data aligned with each other" means something like the following:

IP:          192.168.128.12
MAC:         AF:3G:9F:c9:32:2E
Expires:     Fri Aug 13 20:04:53 2010
Time Left:   11040 seconds

The <pre> tag can be used to maintain alignment.

Minimizing changes to your existing code:

for k,v in string.gmatch(data, "(%w[%w ]*):%s*([%w%p ]+)\n") do t[k] = v end
  • changed first capture to (%w[%w ]*), to avoid leading spaces and to get space in Time Left
  • added %s* after :, to avoid leading spaces in captured values
  • changed %s to space in second capture, to avoid capturing \n
  • fixed typos gmath to gmatch and added ) for capture
坐在坟头思考人生 2024-09-21 18:05:50

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

  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

The pattern below should work for you, provided that:

  1. The IP address is decimal dotted notation.
  2. The MAC address is hexadecimal separated by colons.

Note: The MAC address provided in your question has a 'G' which is not a hexadecimal digit.

Edit: After thinking about your question in detail, I have expanded my answer to show how multiple instances can be captured into a table.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文