在 Lua 列表中搜索未完成的项

请问你知道如何在一个未完成的表中搜索某一项吗?

items = {"0x10_first_one.json", "0x20_second_one.json", "0xFF_thirs_one.json"}

local function locate( table, value )
  for i = 1, #table do
      if table[i] == value then
          print(i)
          return true
      end
  end
  print( value ..' not found' )
  return false
end

locate(items, "0x10" )

我知道我需要搜索 "0x10_first_one.json" 才能得到 1,但通常我没有完整的字符串。我只有 "0x10"。那么我该如何搜索它以便也返回 1 呢?

谢谢

在 Lua 列表中搜索项

原文链接 https://stackoverflow.com/questions/69570246

点赞
stackoverflow用户15175264
stackoverflow用户15175264

我自己找到了解决方案。

function locate(table, value)
  for i = 1, #table do
      test = tostring(table[i])
      if string.match(test, value) ~= nil then
          print(i)
      end
   end
  end

  locate(t, "0x10[_%w]+.json")
2021-10-14 12:17:10