本文介绍了为什么这个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".问题是,当我在所讨论的项目上运行该函数时,就好像该项目不匹配一样.

I am writing an addon for the game World of Warcraft (WoW). It uses lua, with a library of functions specific to the game. I am checking whether a substring can be found in a string. The substring in question is given by the variable ItemType, which in this case contains the string "Two-Handed Swords". The string in which I am checking is given by a table entry and contains "One-Handed Axes, One-Handed Swords, Two-Handed Axes, Two-Handed Swords, Bows, Crossbows, Guns, Wands, Mail, Plate, Shields". The problem is that when I run the function on the item in question, it's acting as if the item doesn't match.

完整代码如下

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"

UnitClass("player") returns { "Druid", "DRUID", 11 } in this case.ItemType i.e. ItemInfo[7] returns "Two-Handed Swords"

推荐答案

-是Lua模式匹配中的魔术字符.您需要使用%对其进行转义.

- is a magic character in Lua pattern matching. You need to escape it with a %.

您还可以使用string.find,它可能会被要求进行纯匹配.

You can also use string.find, which can be asked to do plain matching.

这篇关于为什么这个string.match不能持续工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 19:11