【问题标题】:Lua function for string.match multiple words and numbers用于string.match多个单词和数字的Lua函数
【发布时间】:2022-01-20 15:58:15
【问题描述】:

如果我有一个搜索框并想查找字符串是否包含某些单词(不区分大小写)和/或数字。

search = "Brown lazy 46"
textline = "The quick brown fox jumped over 46 lazy dogs"
if string.match(textline, search) then
  result = textline
end

就像网络搜索一样。

【问题讨论】:

  • 为了解决这个问题,你做了什么?第一个明显的解决方案是遍历您的搜索词并检查它们是否在提供的文本中。
  • This 可能有用

标签: search lua lua-patterns


【解决方案1】:
search = "Brown lazy 46"
textline = "The quick brown fox jumped over 46 lazy dogs"

for item in string.gmatch(search, "%S+") do 
if string.find(textline, string.lower(item)) then
   result = textline
    end
end

你把你要找的单词值打散,然后转换成一个数组。然后您应该循环该数组并检查您的主变量是否在其中。

如果我理解正确你想做什么,这应该可以解决你的问题。

【讨论】:

  • 我只需将其全部更改为小写即可“if string.find(string.lower(textline), string.lower(item)) then”
猜你喜欢
  • 1970-01-01
  • 2011-11-05
  • 2013-06-04
  • 2014-07-10
  • 2020-10-20
  • 2018-02-03
  • 2015-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多