On Wed, May 16, 2012 at 10:01 PM, Sparsh Gupta <spars...@gmail.com> wrote:
>
> I am using Lua gmatch function as per documentation. I dont want the
> parts but just want to see if there was a match or not. I am getting
> back a Lua Iterator but I dont think iterating over it to find if
> match was found or not is the best way. Any other recommendation.
Use ngx.re.match instead of ngx.re.gmatch. See
http://wiki.nginx.org/HttpLuaModule#ngx.re.match
> Code
> is:
>
> if rows then
> local url, n = ngx.re.gsub(ngx.var.arg_u,
> "^(https?:\/\/)(?:w{3}\.)?(.*?)(?:\/(?:home|default|index)\..{3,4}|\/$)?(?:\/)?(?:(?:\?\#.*)|(\?.*?)?(?:#.*)?)$",
> "$1$2$3", "i")
> for i, row in ipairs(rows) do
> ngx.print(row["urls"])
> local iterator = ngx.re.gmatch(url, row["urls"], "i")
> -- HERE I want to check if match happened or not?
> ngx.print(iterator)
> end
> end
>
>
>
> Also, the regex is working fine with PHP and PERL (and bash egrep) but
> not working here and throwing error:
> 2012/05/16 13:59:11 [error] 4754#0: *655 Failed to load Lua inlined
> code: [string "content_by_lua"]:56: invalid escape sequence near
> '"^(https?', client: 122.176.202.238, server: , request: "GET
> /s.php?a=8081&u=http://ankitjain.in HTTP/1.1", host: "lb1"
>
It seems that you're running to double-escaping nightmare documented here:
http://wiki.nginx.org/HttpLuaModule#Special_PCRE_Sequences
The best practice is to use the content_by_lua_file directive instead
of content_by_lua and to put your Lua code into separate .lua files.
See
http://wiki.nginx.org/HttpLuaModule#content_by_lua_file
Using external .lua files also have the merit of bypassing the Lua
code cache during development, see
http://wiki.nginx.org/HttpLuaModule#lua_code_cache
By turning off lua_code_cache during development, you can edit your
.lua files and they will take effect without reloading nginx, just
like php pages. But please keep in mind, it is for development only.
It's a *very* bad idea to turn off the lua code cache in production.
BTW, I'm cc'ing the openresty mailing list. So other people can see
our discussion here ;)
Best regards,
-agentzh