【问题标题】:Lua pattern to remove everything after a wordLua 模式在一个单词后删除所有内容
【发布时间】:2021-06-27 20:30:53
【问题描述】:

如果我有这样的字符串。

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

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

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

【问题讨论】:

  • 基本上,here it is
  • print(("D:/Test/Shit/Server/resources/[Test]/SuperTest"):match("(.*Server)"))
  • 并且do local string=("D:/Test/Shit/Server/resources/[Test]/SuperTest"):match("(.*Server)"):gsub('/','\\') print(string) end 将其更正为反斜杠;-)

标签: lua lua-patterns


【解决方案1】:

使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多