在Lua中从文件中匹配数据的模式匹配。

我被赋予了为 Crysis Wars 创建新的服务器修改的任务。我遇到了一个特定的问题,即它无法读取旧的禁止文件(这是为了保持服务器的一致性所必需的)。Lua 代码本身似乎没有任何错误,但它却没有获取任何数据。

看一看我用于此的下面的代码,你能找出任何错误吗?

这是我用于此的代码:

function rX.CheckBanlist(player)
    local Root = System.GetCVar("sys_root");
    local File = ""..Root.."System/Bansystem/Raptor.xml";
    local FileHnd = io.open(File, "r");
    for line in FileHnd:lines() do
        if (not string.find(line, "User:Read")) then
            System.Log("[rX] File Read Error: System/Raptor/Banfile.xml, The contents are unexpected.");
            return false;
        end
        local Msg, Date, Reason, Type, Domain = string.match(line, "User:Read( '(.*)', { Date='(.*)'; Reason='(.*)'; Typ='(.*)'; Info='(.*)'; } );");
        local rldomain = g_gameRules.game:GetDomain(player.id);
        if (Domain == rldomain) then
            return true;
        else
            return false;
        end
    end
end

此外,实际文件的内容如下,但是我无法在 Lua 中正确地使用“”,这可能是问题吗?

User:Read( "Banned", { Date="31.03.2011"; Reason="WEBSTREAM"; Typ="Inetnum"; Info="COMPUTER.SED.gg"; } );
点赞
用户1190388
用户1190388

你可以使用 Lua 的 [[ 来创建多行字符串,这样就不需要特别处理字符串内的引号了。

另外,在匹配时还需要对 () 进行转义:

local Msg, Date, Reason, Type, Domain = line:match([[User:Read%( "(.-)", { Date="(.+)"; Reason="(.+)"; Typ="(.+)"; Info="(.+)"; } %);]])

最后的结果将和预期一样:http://codepad.org/gN8kSL6H

2013-05-26 11:41:30