为什么这个字符串匹配函数(string.match)不能始终工作呢?
2015-10-21 6:40:51
收藏:0
阅读:77
评论:2
我正在编写一个适用于游戏《魔兽世界》(WoW)的插件。它使用 Lua 编写,并包含游戏特定的函数库。我正在检查一个字符串中是否包含子字符串。所检查的子字符串由变量 ItemType 给出,本例中包含字符串"Two-Handed Swords"。我正在检查的字符串由表项给出,它包含"One-Handed Axes, One-Handed Swords, Two-Handed Axes, Two-Handed Swords, Bows, Crossbows, Guns, Wands, Mail, Plate, Shields"。问题在于,当我对所检查的物品运行该函数时,它似乎认为该物品不匹配。
完整的代码如下:
local NotUsableItemsTable = {
"Wands",
"Daggers, Fist Weapons, Staves, Bows, Crossbows, Guns, Wands",
"One-Handed Maces, Two-Handed Maces, Wands, Plate, Shields",
"Polearms, Staves, Two-Handed Axes, Two-Handed Maces, Two-Handed Swords, Wands, Mail, Plate, Shields",
"Fist Weapons, One-Handed Axes, One-Handed Swords, Polearms, Two-Handed Axes, Two-Handed Maces, Two-Handed Swords, Bows, Crossbows, Guns, Leather, Mail, Plate, Shields",
"Daggers, Fist Weapons, Staves, Bows, Crossbows, Guns, Wands, Shields",
"One-Handed Swords, Polearms, Two-Handed Swords, Bows, Crossbows, Guns, Wands, Plate",
"Fist Weapons, One-Handed Axes, One-Handed Maces, Polearms, Two-Handed Axes, Two-Handed Maces, Two-Handed Swords, Bows, Crossbows, Guns, Leather, Mail, Plate, Shields",
"Fist Weapons, One-Handed Axes, One-Handed Maces, Polearms, Two-Handed Axes, Two-Handed Maces, Two-Handed Swords, Bows Crossbows, Guns, Leather, Mail, Plate, Shields",
"Placeholder String: There is no class corresponding to index 10.",
"One-Handed Axes, One-Handed Swords, Two-Handed Axes, Two-Handed Swords, Bows, Crossbows, Guns, Wands, Mail, Plate, Shields"
}
function IsItemUseableByPlayer(itemID)
if itemID == nil then return nil end
local ClassInfo = {UnitClass("player")}
local NotUsableItemsString = NotUsableItemsTable[ClassInfo[3]]
local ItemInfo = {GetItemInfo(itemID)}
local ItemType = ItemInfo[7]
if string.match(NotUsableItemsString, ItemType) then
return true
else
return false
end
end
在这种情况下,UnitClass("player") 返回 { "Druid", "DRUID", 11 }。
ItemType 即 ItemInfo[7] 返回 "Two-Handed Swords"。
点赞
用户570336
你的字符串中包含Lua模式中具有特殊含义的字符,例如 -。你需要用 % 对其进行转义。
2015-08-19 20:30:07
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
-是 Lua 模式匹配中的一个特殊字符。你需要用%对其进行转义。你也可以使用
string.find,它可以进行普通匹配。