Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua gsub regex替换字符的多次出现_Lua - Fatal编程技术网

Lua gsub regex替换字符的多次出现

Lua gsub regex替换字符的多次出现,lua,Lua,我试图通过删除多个出现的特定字符来修改我的URL,使其干净友好 local function fix_url(str) return str:gsub("[+/=]", {["+"] = "+", ["/"] = "/", ["="] = "="}) --Needs some regex to remove multiple occurances of characters end url = "///index.php????page====about&&&lol===

我试图通过删除多个出现的特定字符来修改我的URL,使其干净友好

local function fix_url(str)
return str:gsub("[+/=]", {["+"] = "+", ["/"] = "/", ["="] = "="}) --Needs some regex to remove multiple occurances of characters
end
url = "///index.php????page====about&&&lol===you"
output = fix_url(url)
我希望实现以下输出:

"/index.php?page=about&lol=you"
"///index.php????page====about&&&lol===you"
但我的输出是:

"/index.php?page=about&lol=you"
"///index.php????page====about&&&lol===you"

gsub是我应该这样做的吗?

这里有一个可能的解决方案(用您喜欢的任何字符类替换%p):


我不知道如何通过调用
gsub
来实现这一点。下面的代码通过为每个字符调用
gsub
一次来实现这一点:

url = "///index.php????page====about&&&lol===you"

function fix_url(s,C)
    for c in C:gmatch(".") do
        s=s:gsub(c.."+",c)
    end
    return s
end

print(fix_url(url,"+/=&?"))

url=url:gsub(([+/=&?])%1“,”\0%0”):gsub((.z%1)”,“”):gsub(%z(.1%1)”,“%1”):gsub(%z.,“”)
Awesome谢谢:)提供的两种解决方案都非常有效,但我将上面的内容标记为答案,因为它更容易用于我需要的内容。谢谢,它工作非常出色,非常容易实现,并且可以为我需要的内容添加更多字符。您只需要小心使用某些字符。例如,不能使用点,因为它将匹配所有内容(+.+)。您应该转义所有标点符号。我会这样写:
函数fix_url(s,C)local m for C in C:gmatch('.')do m=C如果m:match“%W”那么m='%'…m end s=s:gsub(m..'+',C)end返回s end