Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua字符串。匹配。。。匹配一个单词,结尾有可选标点符号_Lua_Match - Fatal编程技术网

Lua字符串。匹配。。。匹配一个单词,结尾有可选标点符号

Lua字符串。匹配。。。匹配一个单词,结尾有可选标点符号,lua,match,Lua,Match,好的,我对Lua语言还不熟悉 我正在尝试运行一些字符串匹配,但是如果在我的“句子词典”中的单词后面有任何标点符号,则匹配无效 我原以为添加一个%p?会匹配“零或一个标点符号”,但事实似乎并非如此 local string_that_matches = string.match(Dictionary[i], textsubmitted..'%p?') 编辑:添加更多信息。以下是完整的例行程序: 嗯。。。嗯,我只是想看看匹配的字符串是不是零。。。如果没有,那么将其添加到一个新的匹配数组中,因为我们

好的,我对Lua语言还不熟悉

我正在尝试运行一些字符串匹配,但是如果在我的“句子词典”中的单词后面有任何标点符号,则匹配无效

我原以为添加一个
%p?
会匹配“零或一个标点符号”,但事实似乎并非如此

local string_that_matches = string.match(Dictionary[i], textsubmitted..'%p?')
编辑:添加更多信息。以下是完整的例行程序:

嗯。。。嗯,我只是想看看匹配的字符串是不是零。。。如果没有,那么将其添加到一个新的匹配数组中,因为我们在这里循环了大约50个项目:

local dictSize = table.maxn(Dictionary)
matches = {} -- new array to hold matches

for i=1,dictSize do -- Loop through dictionary items
    local string_that_matches = string.match(Dictionary[i],textsubmitted..'%p?')
    if string_that_matches ~= nil then
        table.insert(matches, Dictionary[i]) 
    end
end
return matches   

所有这些组合都符合我的预期:

string.match("Good night, boys and girls.", "night")
返回
night

string.match("Good night, boys and girls.", "night%p?")
返回
night,

如果希望匹配不包含(可选)标点符号,请将
textsubmitted
括在括号中:

string.match("Good night, boys and girls.", "(night)%p?")
这将返回
night

下面是一个您可以尝试的完整示例:

local Dictionary = {"Good night, boys and girls."}

function trymatch(textsubmitted)
  local dictSize = table.maxn(Dictionary)
  matches = {} -- new array to hold matches

  for i=1,dictSize do -- Loop through dictionary items
    local string_that_matches = string.match(Dictionary[i],textsubmitted..'%p?')
    if string_that_matches ~= nil then
      table.insert(matches, Dictionary[i]) 
    end
  end
  return matches
end

print(trymatch("Good")[1])
print(trymatch("night")[1])
print(trymatch("boys")[1])
print(trymatch("nothing")[1])
这与预期的一样打印:

Good night, boys and girls.
Good night, boys and girls.
Good night, boys and girls.
nil

你能举例说明哪些不起作用(以及你的预期结果)?当然!Dictionary[i]=“晚安,孩子们。”textsubmitted=“night”不会导致匹配。我发布了一个答案,但这个特定的示例与我的预期相符。你能用不匹配的例子来更新这些问题吗?谢谢,我会这样尝试的。我会在几分钟后给你回复。嗯,我可以在“男孩”上比赛,但我似乎也不能在“好”上比赛。奇怪。对我来说匹配
很好
;我添加了一个完整的示例。好的,谢谢。显然,游戏《战锤在线》(Warhammer Online)有自己的数据类型,称为“wstring”(是的,仍然有一台服务器)。我不确定这是否在起作用。但我目前正在沿着这些路线进行试验,似乎正在取得一些进展。谢谢事实证明,这与那件事无关。我不知道为什么这不起作用,但我把它作为一种解决方法:
localthis\u dict\u string=Dictionary[I]:gsub(([\,;\-!\?\(\))),“”);匹配的本地字符串=string.match(此dict字符串,textsubmitted)