Lua脚本模式匹配问题

发布于 2024-09-13 17:29:27 字数 836 浏览 9 评论 0原文

首先,我在整个脚本编写过程中一直使用这个网站作为参考,这非常棒。我很欣赏这里每个人的有用和知识渊博。考虑到这一点,我有一个关于Lua中的匹配(模式匹配)的问题。我正在编写一个脚本,该脚本本质上从文件中获取输入并将其导入到表中。我正在检查文件中的特定 MAC 地址作为我正在查询的主机。

  if macFile then
     local file = io.open(macFile)

     if file then
    for line in file:lines() do
      local f = line
      i, j = string.find ( f, "%x+" )
      m = string.sub(f, i, j)
      table.insert( macTable, m )
    end
    file:close()
     end

这会将文件解析为我稍后将用于查询的格式。构建表后,我运行一个模式匹配序列,通过迭代表并根据当前迭代匹配模式来尝试匹配表中的 MAC:

local output = {}
t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")
for key,value in next,macTable,nil do
        a, p = string.find ( s, value )
        matchFound = string.sub(s, a, p)
        table.insert( output, matchFound )
end

尽管当我逐行输入时,这不会返回任何输出一个Lua提示,似乎可以工作。我相信变量正在正确传递。有什么建议吗?

First of all, I have been using this site as a reference through the entire scripting process and it has been wonderful. I appreciate how useful and knowledgeable everyone is here. With that in mind, I have a question regarding matching (pattern matching) in Lua. I am writing a script that essentially takes input from a file and imports it into a table. I am checking for specific MAC addresses in the file as the host I am querying.

  if macFile then
     local file = io.open(macFile)

     if file then
    for line in file:lines() do
      local f = line
      i, j = string.find ( f, "%x+" )
      m = string.sub(f, i, j)
      table.insert( macTable, m )
    end
    file:close()
     end

This parses the file into a format I will use to query later. Once the table is built, I run a pattern matching sequence to try and match the MAC from the table by iterating the table and matching the pattern against the current iteration:

local output = {}
t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")
for key,value in next,macTable,nil do
        a, p = string.find ( s, value )
        matchFound = string.sub(s, a, p)
        table.insert( output, matchFound )
end

This doesn't return any output although when I enter it line by line in a Lua prompt, it seems to work. The variables are being passed correctly I believe. Any suggestions?

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

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

发布评论

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

评论(2

时间你老了 2024-09-20 17:29:27

如果您的 macFile 使用如下结构:

012345678900
008967452301
000000000000
ffffffffffff

以下脚本应该可以工作:

macFile = "./macFile.txt"
macTable = {}

if macFile then
    local hFile = io.open(macFile, "r")
    if hFile then
        for line in hFile:lines() do
            local _,_, sMac = line:find("^(%x+)")
            if sMac then
                print("Mac address matched: "..sMac)
                table.insert(macTable, sMac)
            end
        end
        hFile:close()
    end
end

local output = {}
t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")

for k,v in ipairs(macTable) do
    if s == v then
        print("Matched macTable address: "..v)
        table.insert(output, v)
    end
end

If your macFile uses a structure like this:

012345678900
008967452301
000000000000
ffffffffffff

The following script should work:

macFile = "./macFile.txt"
macTable = {}

if macFile then
    local hFile = io.open(macFile, "r")
    if hFile then
        for line in hFile:lines() do
            local _,_, sMac = line:find("^(%x+)")
            if sMac then
                print("Mac address matched: "..sMac)
                table.insert(macTable, sMac)
            end
        end
        hFile:close()
    end
end

local output = {}
t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")

for k,v in ipairs(macTable) do
    if s == v then
        print("Matched macTable address: "..v)
        table.insert(output, v)
    end
end
鱼窥荷 2024-09-20 17:29:27

我刚刚下班,现在无法更深入地了解您的问题,但接下来的两行似乎很奇怪。

t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")

基本上,您将删除字符串 t 中的所有 ':' 字符。之后,您最终会得到 s"000000000000"。这可能不是你想要的?

I am just leaving from work and can't have a deeper look at your problem right now, but the next two lines seem quite odd.

t = "00:00:00:00:00:00"
s = string.gsub( t, ":", "")

Basically you are removing all the ':' characters in the string t. Afterwards you end up with s being "000000000000". This is probably not what you want?

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