Lua 模式删除单词后的所有内容

问题描述

如果我有这样的字符串。

local string = "D:/Test/Stuff/Server/resources/[Test]/SuperTest"

我将如何删除“服务器”一词之后的所有内容,使其最终看起来像这样

local string = "D:/Test/Stuff/Server"

解决方法

使用 str 而不是 string 来不影响真正的字符串库

local str = "D:/Test/Stuff/Server/resources/[Test]/SuperTest"

这里有一个解决方案

str = str:match("(.*Server)")

或使用do...end以避免阴影

do
    local string = ("D:/Test/Stuff/Server/resources/[Test]/SuperTest"):match("(.*Server)")
end