为什么这 string.match 不能一直有效?

Why isn't this string.match consistently working?

我正在为魔兽世界 (WoW) 游戏编写插件。它使用 lua,带有特定于游戏的函数库。我正在检查是否可以在字符串中找到子字符串。有问题的子字符串由变量 ItemType 给出,在本例中它包含字符串 "Two-Handed Swords"。我正在检查的字符串由 table 条目给出并包含 "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") returns { "Druid", "DRUID", 11 } 在这种情况下。 ItemTypeItemInfo[7] returns "Two-Handed Swords"

-是Lua模式匹配中的魔法字符。您需要使用 %.

对其进行转义

也可以使用string.find,可以要求做平原匹配。

您的字符串包含 Lua 模式中具有特殊含义的字符,在本例中为 -。你必须用 %.

来逃避它