为什么这个字符串匹配函数(string.match)不能始终工作呢?

我正在编写一个适用于游戏《魔兽世界》(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 }ItemTypeItemInfo[7] 返回 "Two-Handed Swords"。

点赞
用户107090
用户107090

- 是 Lua 模式匹配中的一个特殊字符。你需要用 % 对其进行转义。

你也可以使用 string.find,它可以进行普通匹配。

2015-08-19 20:29:42
用户570336
用户570336

你的字符串中包含Lua模式中具有特殊含义的字符,例如 -。你需要用 % 对其进行转义。

2015-08-19 20:30:07