Lua脚本模式匹配问题
首先,我在整个脚本编写过程中一直使用这个网站作为参考,这非常棒。我很欣赏这里每个人的有用和知识渊博。考虑到这一点,我有一个关于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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的 macFile 使用如下结构:
以下脚本应该可以工作:
If your macFile uses a structure like this:
The following script should work:
我刚刚下班,现在无法更深入地了解您的问题,但接下来的两行似乎很奇怪。
基本上,您将删除字符串
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.
Basically you are removing all the
':'
characters in the stringt
. Afterwards you end up withs
being"000000000000"
. This is probably not what you want?