ngx.re.match多行匹配的困惑

154 views
Skip to first unread message

lhmwzy

unread,
Mar 4, 2013, 12:44:25 AM3/4/13
to open...@googlegroups.com
像如下语句
body1=[[
<html>
<table border="0" width="100%" cellspacing="0" cellpadding="0" class="TableLine"   align="center">
        dsfkljsdf
sdf
sdf
sdfs
<form method=Post action="rl_SmallClass.asp?BName=>ldsfjksdlfkjsf
sdfsdffsd
</html>
]]

local res=ngx.re.match(body1,[[<html>(.*)</html>]],"imjxd")
为啥没有匹配结果,我哪里写错了?
error.log:
attempt to index local 'res' (a nil value)

nginx/sbin/nginx -V
nginx version: ngx_openresty/1.2.7.1
built by gcc 4.2.1 20070719  [FreeBSD]
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --add-module=../ngx_devel_kit-0.2.18 --add-module=../echo-nginx-module-0.42 --add-module=../xss-nginx-module-0.03rc9 --add-module=../ngx_coolkit-0.2rc1 --add-module=../set-misc-nginx-module-0.22rc8 --add-module=../form-input-nginx-module-0.07 --add-module=../encrypted-session-nginx-module-0.02 --add-module=../drizzle-nginx-module-0.1.4 --add-module=../srcache-nginx-module-0.19 --add-module=../ngx_lua-0.7.16 --add-module=../headers-more-nginx-module-0.19 --add-module=../array-var-nginx-module-0.03rc1 --add-module=../memc-nginx-module-0.13rc3 --add-module=../redis2-nginx-module-0.09 --add-module=../redis-nginx-module-0.3.6 --add-module=../auth-request-nginx-module-0.2 --add-module=../rds-json-nginx-module-0.12rc10 --add-module=../rds-csv-nginx-module-0.05rc2 --with-ld-opt=-Wl,-rpath,/usr/local/libdrizzle/lib:/usr/local/openresty/luajit/lib --with-http_ssl_module



agentzh

unread,
Mar 4, 2013, 12:58:22 AM3/4/13
to open...@googlegroups.com
Hello!

On Sun, Mar 3, 2013 at 9:44 PM, lhmwzy wrote:
> 像如下语句
> body1=[[
> <html>
> <table border="0" width="100%" cellspacing="0" cellpadding="0"
> class="TableLine" align="center">
> dsfkljsdf
> sdf
> sdf
> sdfs
> <form method=Post action="rl_SmallClass.asp?BName=>ldsfjksdlfkjsf
> sdfsdffsd
> </html>
> ]]
>
> local res=ngx.re.match(body1,[[<html>(.*)</html>]],"imjxd")
> 为啥没有匹配结果,我哪里写错了?
> error.log:
> attempt to index local 'res' (a nil value)
>

在 PCRE 默认模式下,正则中的圆点并不会匹配换行符。你需要指定 s 正则选项,即:

local res=ngx.re.match(body1,[[<html>(.*)</html>]],"is")

当然,同时指定 jo 选项是很好的习惯,可以极大地提升性能,即

local res=ngx.re.match(body1,[[<html>(.*)</html>]],"isjo")

你代码中的 xmd 都不是必须的。PCRE 的 DFA 模式并没什么性能上的优势。

Best regards,
-agentzh

lhmwzy

unread,
Mar 4, 2013, 1:11:48 AM3/4/13
to open...@googlegroups.com
我以为m是多行匹配模式,看来是我想错了。



--
--
邮件自: 列表“openresty”,专用于技术讨论!
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
详情: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
建议: 提问的智慧 http://wiki.woodpecker.org.cn/moin/AskForHelp
教程: http://agentzh.org/misc/nginx/agentzh-nginx-tutorials-zhcn.html

---
您收到此邮件是因为您订阅了 Google 网上论坛的“openresty”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 openresty+...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out



lhmwzy

unread,
Mar 4, 2013, 1:34:23 AM3/4/13
to open...@googlegroups.com
如下模式 ,还是匹配不到,真是奇怪

body1=[[
idfsfsdf
dfsdfsdf

<table border="0" width="100%" cellspacing="0" cellpadding="0" class="TableLine"   align="center">1
fsdfsdfds
fsdfsd
fsdf
sdfsd
fsd
fs
df
sd
i<form method=Post action="rl_SmallClass.asp?BName=4444444444444444
fdsfdsfds
]]

local res=ngx.re.match(body1,[[.*<table border="0" width="100%" cellspacing="0" cellpadding="0" class="TableLine"   align="center">(.*)<form method=Post action="rl_SmallClass.asp?BName=.*]],"isjo")

lhmwzy

unread,
Mar 4, 2013, 1:38:51 AM3/4/13
to open...@googlegroups.com
应该是 l_SmallClass.asp?BName 这里面的"?"的原因
Reply all
Reply to author
Forward
0 new messages