【原创】OpenResty Lua 中使用 Nginx 正则表达式

Lua 自带的正则表达式确实有点另类,不支持 “|” 而且正则语法不好适应,想用Nginx原生的正则表达式就需要在Lua中借助Nginx内置对象 ngx.re

 

正则在 nginx.conf 文件中错误的使用方法:

    # nginx.conf
    ? location /test {
    ?     content_by_lua '
    ?         local regex = "\d+"  -- THIS IS WRONG!!
    ?         local m = ngx.re.match("hello, 1234", regex)
    ?         if m then ngx.say(m[0]) else ngx.say("not matched!") end
    ?     ';
    ? }
    # evaluates to "not matched!"

 nginx.conf 文件中正确使用方法 记得是\\\\才能转义

    # nginx.conf
    location /test {
        content_by_lua '
            local regex = "\\\\d+"
            local m = ngx.re.match("hello, 1234", regex)
            if m then ngx.say(m[0]) else ngx.say("not matched!") end
        ';
    }
    # evaluates to "1234"

 

可以使用Lua中特有的 [[ 正则 ]] 定义符号

    # nginx.conf
    location /test {
        content_by_lua '
            local regex = [[\\d+]]
            local m = ngx.re.match("hello, 1234", regex)
            if m then ngx.say(m[0]) else ngx.say("not matched!") end
        ';
    }
    # evaluates to "1234"

这个自己翻译了

Here, [[\\d+]] is stripped down to [[\d+]] by the Nginx config file parser and this is processed correctly.

Note that a longer from of the long bracket, [=[...]=], may be required if the regex pattern contains [...] sequences. The [=[...]=] form may be used as the default form if desired.

    # nginx.conf
    location /test {
        content_by_lua '
            local regex = [=[[0-9]+]=]
            local m = ngx.re.match("hello, 1234", regex)
            if m then ngx.say(m[0]) else ngx.say("not matched!") end
        ';
    }
    # evaluates to "1234"

如果采用 content_by_lua_file 'test.lua'  \\\\d+ 简化为 \\d+

    -- test.lua
    local regex = "\\d+"
    local m = ngx.re.match("hello, 1234", regex)
    if m then ngx.say(m[0]) else ngx.say("not matched!") end
    -- evaluates to "1234"

如果采用 content_by_lua_file 'test.lua'   [[\\d+]] 简化为  [[\d+]]

    -- test.lua
    local regex = [[\d+]]
    local m = ngx.re.match("hello, 1234", regex)
    if m then ngx.say(m[0]) else ngx.say("not matched!") end
    -- evaluates to "1234"

来源:http://wiki.nginx.org/HttpLuaModule

 

你可能感兴趣的:(【原创】OpenResty Lua 中使用 Nginx 正则表达式)