Lua string.gmatch 模式匹配多个逗号

该 URI 包含用三个连续逗号分隔的值。

例如:/path?first,,,second,,,third,value,,,fourth

我想要迭代这些值并按如下格式打印出来:

first
second
third,value
fourth

这个示例只找到一个逗号,第三个值因为包含了单个逗号而匹配失败。

for word in string.gmatch(ngx.var.request_uri, "[^,]+") do ngx.say(word) end

这个也不工作:

for word in string.gmatch(ngx.var.request_uri, "[^,]{3}") do ngx.say(word) end

在这个示例中,使用正则表达式的正确模式是什么?

点赞
用户3832970
用户3832970

你可以删除到?为止,然后将,,,替换为不太可能出现在字符串中的字符(比如\0,正如Egor Skriptunoff所建议的),然后使用"[^\0]+"模式提取你需要的项目。

参见在线 Lua 演示

local s = "/path?first,,,second,,,third,value,,,fourth"
s = s:gsub("^[^?]*%?", ""):gsub(",,,", "\0")
for word in string.gmatch(s, "[^\0]+") do print(word) end

输出结果:

first
second
third,value
fourth

因此,使用gsub("^[^?]*%?", ""),从字符串开头到第一个?件文本和?一起被删除,然后gsub(",,,", "\0"),,,替换为一个零字节字符,而string.gmatch(s, "[^\0]+")执行期望的多匹配。

LuaJIT 版本

[^\0] 在 LuaJIT 中无效,因此应该使用%Z+模式进行gmatch匹配,该模式匹配除零字节字符之外的一个或多个字符(%z是表示0的字符,参见文档)。

参见测试片段:

> s = "/path?first,,,second,,,third,value,,,fourth"
> s = s:gsub("^[^?]*%?", ""):gsub(",,,", "\0")
> for word in string.gmatch(s, "%Z+") do print(word) end
first
second
third,value
fourth
2019-03-04 21:26:39
用户7396148
用户7396148

我相信这个可以满足你的需要:

local function process_param(s)
    print(s)
end

local path = "/path?first,,,second,,,third,value,,,fourth"
local first = string.match(path, "?([^,]+[,]?[^,]+)")
process_param(first)

for word in string.gmatch(path, ",,,([^,]+[,]?[^,]+)") do
    process_param(word)
end

这个示例需要单独的步骤来获取first的值,因为它没有前导的,,,。我用(捕获了字符串中需要的部分,这样可以指定周围的字符而不包括它们在输出中。我使用了[,]?来允许单个逗号出现在被捕获的字符串中,从而让结果返回third,value

这会产生:

first
second
third,value
fourth

资源:understanding_lua_patterns

2019-03-04 21:54:13