【问题标题】:Get all matching Strings in a Table获取表中所有匹配的字符串
【发布时间】:2017-10-20 00:36:41
【问题描述】:

我有一个像“function *()”这样的搜索字符串。 * 是通配符。我想要一个包含所有匹配字符串列表的表格。

例如 如果我使用搜索字符串“function *()”

搜索此字符串
function one()
  print("Hello")
end

local function two()
  print("World")
end

它应该返回表 {"function one()","function two()"}。如何在 LUA 中做到这一点?

【问题讨论】:

  • retrurn {one,two}

标签: lua


【解决方案1】:
local function search(text, searchstring)
   local result = {}
   local pattern = searchstring:gsub("*", "\0"):gsub("%p", "%%%0"):gsub("%z", ".-")
   for w in text:gmatch(pattern) do
      table.insert(result, w)
   end
   return result
end

用法:

local text = [[
function one()
  print("Hello")
end

local function two()
  print("World")
end
]]

local searchstring = "function *()"

local result = search(text, searchstring)   --> {"function one()", "function two()"}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 2014-08-21
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多