有两种可能的修改方法,以 regex.lua 中的 re_match_helper 为例,want_caps == false 时
1. 将此函数的输出扩展为不仅仅返回 s, e,而是返回所有的结果集偏移,具体到函数中,就是在以下这段中
if not want_caps then
return compiled.captures[0] + 1, compiled.captures[1]
end
将 compiled.captures 修改后完整输出,当用户需要 m[1]时,以 s0, e0, s1, e1 = ngx.re.find() 形式调用
或 2. 在 re.find 中添加一个参数 n,输出指定的 m[n],并把参数传递给 re_match_helper,这样可以直接把
上面举例的三行代码改成
if not want_caps then
return compiled.captures[n] + 1, compiled.captures[n+1]
end
我感觉两种方式各有优缺点,更倾向于第二种。