请教关于ngx.re.match 匹配的问题 貌似不能全局匹配,只能匹配单个.请帮忙看我的代码

84 views
Skip to first unread message

g_l...@qq.com

unread,
Oct 26, 2016, 4:12:36 AM10/26/16
to openresty
body_filter_by_lua_block {
local sum=""
 for m in string.gmatch(ngx.arg[1],[[<img([^>]*)]]) do
        if m then
                        sum=sum .. m .. "\n"
        end
end
ngx.arg[1]=sum
 }

请问大神们 怎么把 string.gmatch 用 ngx.re.match 来实现。
我试了好多次都没有办法。

xiaooloong

unread,
Oct 26, 2016, 4:42:32 AM10/26/16
to openresty
ngx.re.gmatch
https://github.com/openresty/lua-nginx-module#ngxregmatch

在 2016年10月26日星期三 UTC+8下午4:12:36,g_l...@qq.com写道:

g_l...@qq.com

unread,
Oct 26, 2016, 5:07:37 AM10/26/16
to openresty
ngx.re.gmatch 我也测试过了也不可以
gmatch 只不过是返回了一个迭代器而已。我循环里面的内容。
没有达到我和我这代码一样的功能

我这代码是提取页面所有的img 标签
而用 ngx.re.match 和 ngx.re.gmatch 貌似只返回一个匹配的条件 就不在往下匹配了
根本不能全局匹配
我想要的功能
python:
In [1]: import re

In [2]: a="abcdefgabcdefgabcdefg"

In [3]: re.fin
re.findall   re.finditer  

In [3]: re.findall("a")

In [4]: re.findall("a",a)
Out[4]: ['a', 'a', 'a']

In [5]: 

不是这种
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> a="abcdefgabcdefgabcdefg"
> string.match(a,"a")
> print(string.match(a,"a"))
a


在 2016年10月26日星期三 UTC+8下午4:42:32,xiaooloong写道:

xiaooloong

unread,
Oct 26, 2016, 5:17:11 AM10/26/16
to openresty
local a = 'abcdefgabcdefgabcdefg'
local m = ngx.re.gmatch(a, [[a]])
local t = ''
for v in m do
    t = t .. v[0]
end
ngx.say(t)



在 2016年10月26日星期三 UTC+8下午5:07:37,g_l...@qq.com写道:

g_l...@qq.com

unread,
Oct 26, 2016, 9:16:52 PM10/26/16
to openresty
搞定了。谢谢你。是我的用法不对。

在 2016年10月26日星期三 UTC+8下午5:17:11,xiaooloong写道:
Reply all
Reply to author
Forward
0 new messages