Merge branch 'ngx-re-no-exceptions'

This commit is contained in:
agentzh (Yichun Zhang) 2013-03-20 17:06:05 -07:00
Родитель 3ad47790ed 3ea32c9693
Коммит 4751625f89
17 изменённых файлов: 716 добавлений и 254 удалений

117
README
Просмотреть файл

@ -3550,7 +3550,7 @@ Nginx API for Lua
otherwise.
ngx.re.match
syntax: *captures = ngx.re.match(subject, regex, options?, ctx?)*
syntax: *captures, err = ngx.re.match(subject, regex, options?, ctx?)*
context: *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*,
header_filter_by_lua*, body_filter_by_lua*, log_by_lua**
@ -3559,18 +3559,29 @@ Nginx API for Lua
expression "regex" with the optional "options".
Only the first occurrence of the match is returned, or "nil" if no match
is found. In case of fatal errors, like seeing bad "UTF-8" sequences in
"UTF-8" mode, a Lua exception will be raised.
is found. In case of errors, like seeing a bad regular expression or
exceeding the PCRE stack limit, `nil` and a string describing the error
will be returned.
When a match is found, a Lua table "captures" is returned, where
"captures[0]" holds the whole substring being matched, and "captures[1]"
holds the first parenthesized sub-pattern's capturing, "captures[2]" the
second, and so on.
local m = ngx.re.match("hello, 1234", "[0-9]+")
-- m[0] == "1234"
local m, err = ngx.re.match("hello, 1234", "[0-9]+")
if m then
-- m[0] == "1234"
local m = ngx.re.match("hello, 1234", "([0-9])[0-9]+")
else
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
ngx.say("match not found")
end
local m, err = ngx.re.match("hello, 1234", "([0-9])[0-9]+")
-- m[0] == "1234"
-- m[1] == "1"
@ -3578,7 +3589,7 @@ Nginx API for Lua
returned in the same Lua table as key-value pairs as the numbered
captures.
local m = ngx.re.match("hello, 1234", "([0-9])(?<remaining>[0-9]+)")
local m, err = ngx.re.match("hello, 1234", "([0-9])(?<remaining>[0-9]+)")
-- m[0] == "1234"
-- m[1] == "1"
-- m[2] == "234"
@ -3587,7 +3598,7 @@ Nginx API for Lua
Unmatched subpatterns will have "nil" values in their "captures" table
fields.
local m = ngx.re.match("hello, world", "(world)|(hello)|(?<named>howdy)")
local m, err = ngx.re.match("hello, world", "(world)|(hello)|(?<named>howdy)")
-- m[0] == "hello"
-- m[1] == nil
-- m[2] == "hello"
@ -3660,12 +3671,12 @@ Nginx API for Lua
match. When match fails, the "ctx" table will be left intact.
local ctx = {}
local m = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
local m, err = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
-- m[0] = "1234"
-- ctx.pos == 4
local ctx = { pos = 2 }
local m = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
local m, err = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
-- m[0] = "34"
-- ctx.pos == 4
@ -3690,7 +3701,7 @@ Nginx API for Lua
This feature was introduced in the "v0.2.1rc11" release.
ngx.re.gmatch
syntax: *iterator = ngx.re.gmatch(subject, regex, options?)*
syntax: *iterator, err = ngx.re.gmatch(subject, regex, options?)*
context: *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*,
header_filter_by_lua*, body_filter_by_lua*, log_by_lua**
@ -3699,17 +3710,57 @@ Nginx API for Lua
let the user programmer iterate all the matches over the "<subject>"
string argument with the PCRE "regex".
In case of errors, like seeing an ill-formed regular expression, `nil`
and a string describing the error will be returned.
Here is a small example to demonstrate its basic usage:
local iterator = ngx.re.gmatch("hello, world!", "([a-z]+)", "i")
local iterator, err = ngx.re.gmatch("hello, world!", "([a-z]+)", "i")
if not iterator then
ngx.log(ngx.ERR, "error: ", err)
return
end
local m
m = iterator() -- m[0] == m[1] == "hello"
m = iterator() -- m[0] == m[1] == "world"
m = iterator() -- m == nil
m, err = iterator() -- m[0] == m[1] == "hello"
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
More often we just put it into a Lua "for" loop:
m, err = iterator() -- m[0] == m[1] == "world"
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
for m in ngx.re.gmatch("hello, world!", "([a-z]+)", "i") do
m, err = iterator() -- m == nil
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
More often we just put it into a Lua loop:
local it, err = ngx.re.gmatch("hello, world!", "([a-z]+)", "i")
if not it then
ngx.log(ngx.ERR, "error: ", err)
return
end
while true do
local m, err = it()
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
if not m then
-- no match found (any more)
break
end
-- found a match
ngx.say(m[0])
ngx.say(m[1])
end
@ -3727,7 +3778,7 @@ Nginx API for Lua
This feature was first introduced in the "v0.2.1rc12" release.
ngx.re.sub
syntax: *newstr, n = ngx.re.sub(subject, regex, replace, options?)*
syntax: *newstr, n, err = ngx.re.sub(subject, regex, replace, options?)*
context: *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*,
header_filter_by_lua*, body_filter_by_lua*, log_by_lua**
@ -3738,31 +3789,37 @@ Nginx API for Lua
meaning as in ngx.re.match.
This method returns the resulting new string as well as the number of
successful substitutions, or throw out a Lua exception when an error
occurred (syntax errors in the "<replace>" string argument, for
example).
successful substitutions. In case of failures, like syntax errors in the
regular expressions or the "<replace>" string argument, it will return
`nil` and a string describing the error.
When the "replace" is a string, then it is treated as a special template
for string replacement. For example,
local newstr, n = ngx.re.sub("hello, 1234", "([0-9])[0-9]", "[$0][$1]")
local newstr, n, err = ngx.re.sub("hello, 1234", "([0-9])[0-9]", "[$0][$1]")
if newstr then
-- newstr == "hello, [12][1]34"
-- n == 1
else
ngx.log(ngx.ERR, "error: ", err)
return
end
where $0 referring to the whole substring matched by the pattern and $1
referring to the first parenthesized capturing substring.
Curly braces can also be used to disambiguate variable names from the
background string literals:
local newstr, n = ngx.re.sub("hello, 1234", "[0-9]", "${0}00")
local newstr, n, err = ngx.re.sub("hello, 1234", "[0-9]", "${0}00")
-- newstr == "hello, 10034"
-- n == 1
Literal dollar sign characters ("$") in the "replace" string argument
can be escaped by another dollar sign, for instance,
local newstr, n = ngx.re.sub("hello, 1234", "[0-9]", "$$")
local newstr, n, err = ngx.re.sub("hello, 1234", "[0-9]", "$$")
-- newstr == "hello, $234"
-- n == 1
@ -3778,7 +3835,7 @@ Nginx API for Lua
local func = function (m)
return "[" .. m[0] .. "][" .. m[1] .. "]"
end
local newstr, n = ngx.re.sub("hello, 1234", "( [0-9] ) [0-9]", func, "x")
local newstr, n, err = ngx.re.sub("hello, 1234", "( [0-9] ) [0-9]", func, "x")
-- newstr == "hello, [12][1]34"
-- n == 1
@ -3800,14 +3857,20 @@ Nginx API for Lua
Here is some examples:
local newstr, n = ngx.re.gsub("hello, world", "([a-z])[a-z]+", "[$0,$1]", "i")
local newstr, n, err = ngx.re.gsub("hello, world", "([a-z])[a-z]+", "[$0,$1]", "i")
if newstr then
-- newstr == "[hello,h], [world,w]"
-- n == 2
else
ngx.log(ngx.ERR, "error: ", err)
return
end
local func = function (m)
return "[" .. m[0] .. "," .. m[1] .. "]"
end
local newstr, n = ngx.re.gsub("hello, world", "([a-z])[a-z]+", func, "i")
local newstr, n, err = ngx.re.gsub("hello, world", "([a-z])[a-z]+", func, "i")
-- newstr == "[hello,h], [world,w]"
-- n == 2

Просмотреть файл

@ -3291,23 +3291,33 @@ Returns `true` if the current request is an nginx subrequest, or `false` otherwi
ngx.re.match
------------
**syntax:** *captures = ngx.re.match(subject, regex, options?, ctx?)*
**syntax:** *captures, err = ngx.re.match(subject, regex, options?, ctx?)*
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua**
Matches the `subject` string using the Perl compatible regular expression `regex` with the optional `options`.
Only the first occurrence of the match is returned, or `nil` if no match is found. In case of fatal errors, like seeing bad `UTF-8` sequences in `UTF-8` mode, a Lua exception will be raised.
Only the first occurrence of the match is returned, or `nil` if no match is found. In case of errors, like seeing a bad regular expression or exceeding the PCRE stack limit, `nil` and a string describing the error will be returned.
When a match is found, a Lua table `captures` is returned, where `captures[0]` holds the whole substring being matched, and `captures[1]` holds the first parenthesized sub-pattern's capturing, `captures[2]` the second, and so on.
local m = ngx.re.match("hello, 1234", "[0-9]+")
-- m[0] == "1234"
local m, err = ngx.re.match("hello, 1234", "[0-9]+")
if m then
-- m[0] == "1234"
else
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
ngx.say("match not found")
end
local m = ngx.re.match("hello, 1234", "([0-9])[0-9]+")
local m, err = ngx.re.match("hello, 1234", "([0-9])[0-9]+")
-- m[0] == "1234"
-- m[1] == "1"
@ -3316,7 +3326,7 @@ Named captures are also supported since the `v0.7.14` release
and are returned in the same Lua table as key-value pairs as the numbered captures.
local m = ngx.re.match("hello, 1234", "([0-9])(?<remaining>[0-9]+)")
local m, err = ngx.re.match("hello, 1234", "([0-9])(?<remaining>[0-9]+)")
-- m[0] == "1234"
-- m[1] == "1"
-- m[2] == "234"
@ -3326,7 +3336,7 @@ and are returned in the same Lua table as key-value pairs as the numbered captur
Unmatched subpatterns will have `nil` values in their `captures` table fields.
local m = ngx.re.match("hello, world", "(world)|(hello)|(?<named>howdy)")
local m, err = ngx.re.match("hello, world", "(world)|(hello)|(?<named>howdy)")
-- m[0] == "hello"
-- m[1] == nil
-- m[2] == "hello"
@ -3396,14 +3406,14 @@ The optional fourth argument, `ctx`, can be a Lua table holding an optional `pos
local ctx = {}
local m = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
local m, err = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
-- m[0] = "1234"
-- ctx.pos == 4
local ctx = { pos = 2 }
local m = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
local m, err = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
-- m[0] = "34"
-- ctx.pos == 4
@ -3424,26 +3434,65 @@ This feature was introduced in the `v0.2.1rc11` release.
ngx.re.gmatch
-------------
**syntax:** *iterator = ngx.re.gmatch(subject, regex, options?)*
**syntax:** *iterator, err = ngx.re.gmatch(subject, regex, options?)*
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua**
Similar to [ngx.re.match](http://wiki.nginx.org/HttpLuaModule#ngx.re.match), but returns a Lua iterator instead, so as to let the user programmer iterate all the matches over the `<subject>` string argument with the PCRE `regex`.
In case of errors, like seeing an ill-formed regular expression, `nil` and a string describing the error will be returned.
Here is a small example to demonstrate its basic usage:
local iterator = ngx.re.gmatch("hello, world!", "([a-z]+)", "i")
local iterator, err = ngx.re.gmatch("hello, world!", "([a-z]+)", "i")
if not iterator then
ngx.log(ngx.ERR, "error: ", err)
return
end
local m
m = iterator() -- m[0] == m[1] == "hello"
m = iterator() -- m[0] == m[1] == "world"
m = iterator() -- m == nil
m, err = iterator() -- m[0] == m[1] == "hello"
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
m, err = iterator() -- m[0] == m[1] == "world"
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
m, err = iterator() -- m == nil
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
More often we just put it into a Lua `for` loop:
More often we just put it into a Lua loop:
for m in ngx.re.gmatch("hello, world!", "([a-z]+)", "i") do
local it, err = ngx.re.gmatch("hello, world!", "([a-z]+)", "i")
if not it then
ngx.log(ngx.ERR, "error: ", err)
return
end
while true do
local m, err = it()
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
if not m then
-- no match found (any more)
break
end
-- found a match
ngx.say(m[0])
ngx.say(m[1])
end
@ -3459,28 +3508,34 @@ This feature was first introduced in the `v0.2.1rc12` release.
ngx.re.sub
----------
**syntax:** *newstr, n = ngx.re.sub(subject, regex, replace, options?)*
**syntax:** *newstr, n, err = ngx.re.sub(subject, regex, replace, options?)*
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua**
Substitutes the first match of the Perl compatible regular expression `regex` on the `subject` argument string with the string or function argument `replace`. The optional `options` argument has exactly the same meaning as in [ngx.re.match](http://wiki.nginx.org/HttpLuaModule#ngx.re.match).
This method returns the resulting new string as well as the number of successful substitutions, or throw out a Lua exception when an error occurred (syntax errors in the `<replace>` string argument, for example).
This method returns the resulting new string as well as the number of successful substitutions. In case of failures, like syntax errors in the regular expressions or the `<replace>` string argument, it will return `nil` and a string describing the error.
When the `replace` is a string, then it is treated as a special template for string replacement. For example,
local newstr, n = ngx.re.sub("hello, 1234", "([0-9])[0-9]", "[$0][$1]")
local newstr, n, err = ngx.re.sub("hello, 1234", "([0-9])[0-9]", "[$0][$1]")
if newstr then
-- newstr == "hello, [12][1]34"
-- n == 1
else
ngx.log(ngx.ERR, "error: ", err)
return
end
where `$0` referring to the whole substring matched by the pattern and `$1` referring to the first parenthesized capturing substring.
Curly braces can also be used to disambiguate variable names from the background string literals:
local newstr, n = ngx.re.sub("hello, 1234", "[0-9]", "${0}00")
local newstr, n, err = ngx.re.sub("hello, 1234", "[0-9]", "${0}00")
-- newstr == "hello, 10034"
-- n == 1
@ -3488,7 +3543,7 @@ Curly braces can also be used to disambiguate variable names from the background
Literal dollar sign characters (`$`) in the `replace` string argument can be escaped by another dollar sign, for instance,
local newstr, n = ngx.re.sub("hello, 1234", "[0-9]", "$$")
local newstr, n, err = ngx.re.sub("hello, 1234", "[0-9]", "$$")
-- newstr == "hello, $234"
-- n == 1
@ -3501,7 +3556,7 @@ When the `replace` argument is of type "function", then it will be invoked with
local func = function (m)
return "[" .. m[0] .. "][" .. m[1] .. "]"
end
local newstr, n = ngx.re.sub("hello, 1234", "( [0-9] ) [0-9]", func, "x")
local newstr, n, err = ngx.re.sub("hello, 1234", "( [0-9] ) [0-9]", func, "x")
-- newstr == "hello, [12][1]34"
-- n == 1
@ -3523,16 +3578,22 @@ Just like [ngx.re.sub](http://wiki.nginx.org/HttpLuaModule#ngx.re.sub), but does
Here is some examples:
local newstr, n = ngx.re.gsub("hello, world", "([a-z])[a-z]+", "[$0,$1]", "i")
local newstr, n, err = ngx.re.gsub("hello, world", "([a-z])[a-z]+", "[$0,$1]", "i")
if newstr then
-- newstr == "[hello,h], [world,w]"
-- n == 2
else
ngx.log(ngx.ERR, "error: ", err)
return
end
local func = function (m)
return "[" .. m[0] .. "," .. m[1] .. "]"
end
local newstr, n = ngx.re.gsub("hello, world", "([a-z])[a-z]+", func, "i")
local newstr, n, err = ngx.re.gsub("hello, world", "([a-z])[a-z]+", func, "i")
-- newstr == "[hello,h], [world,w]"
-- n == 2

Просмотреть файл

@ -3175,23 +3175,33 @@ Parse the http time string (as returned by [[#ngx.http_time|ngx.http_time]]) int
Returns <code>true</code> if the current request is an nginx subrequest, or <code>false</code> otherwise.
== ngx.re.match ==
'''syntax:''' ''captures = ngx.re.match(subject, regex, options?, ctx?)''
'''syntax:''' ''captures, err = ngx.re.match(subject, regex, options?, ctx?)''
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*''
Matches the <code>subject</code> string using the Perl compatible regular expression <code>regex</code> with the optional <code>options</code>.
Only the first occurrence of the match is returned, or <code>nil</code> if no match is found. In case of fatal errors, like seeing bad <code>UTF-8</code> sequences in <code>UTF-8</code> mode, a Lua exception will be raised.
Only the first occurrence of the match is returned, or <code>nil</code> if no match is found. In case of errors, like seeing a bad regular expression or exceeding the PCRE stack limit, `nil` and a string describing the error will be returned.
When a match is found, a Lua table <code>captures</code> is returned, where <code>captures[0]</code> holds the whole substring being matched, and <code>captures[1]</code> holds the first parenthesized sub-pattern's capturing, <code>captures[2]</code> the second, and so on.
<geshi lang="lua">
local m = ngx.re.match("hello, 1234", "[0-9]+")
-- m[0] == "1234"
local m, err = ngx.re.match("hello, 1234", "[0-9]+")
if m then
-- m[0] == "1234"
else
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
ngx.say("match not found")
end
</geshi>
<geshi lang="lua">
local m = ngx.re.match("hello, 1234", "([0-9])[0-9]+")
local m, err = ngx.re.match("hello, 1234", "([0-9])[0-9]+")
-- m[0] == "1234"
-- m[1] == "1"
</geshi>
@ -3200,7 +3210,7 @@ Named captures are also supported since the <code>v0.7.14</code> release
and are returned in the same Lua table as key-value pairs as the numbered captures.
<geshi lang="lua">
local m = ngx.re.match("hello, 1234", "([0-9])(?<remaining>[0-9]+)")
local m, err = ngx.re.match("hello, 1234", "([0-9])(?<remaining>[0-9]+)")
-- m[0] == "1234"
-- m[1] == "1"
-- m[2] == "234"
@ -3210,7 +3220,7 @@ and are returned in the same Lua table as key-value pairs as the numbered captur
Unmatched subpatterns will have <code>nil</code> values in their <code>captures</code> table fields.
<geshi lang="lua">
local m = ngx.re.match("hello, world", "(world)|(hello)|(?<named>howdy)")
local m, err = ngx.re.match("hello, world", "(world)|(hello)|(?<named>howdy)")
-- m[0] == "hello"
-- m[1] == nil
-- m[2] == "hello"
@ -3280,14 +3290,14 @@ The optional fourth argument, <code>ctx</code>, can be a Lua table holding an op
<geshi lang="lua">
local ctx = {}
local m = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
local m, err = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
-- m[0] = "1234"
-- ctx.pos == 4
</geshi>
<geshi lang="lua">
local ctx = { pos = 2 }
local m = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
local m, err = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
-- m[0] = "34"
-- ctx.pos == 4
</geshi>
@ -3307,26 +3317,65 @@ To confirm that PCRE JIT is enabled, activate the Nginx debug log by adding the
This feature was introduced in the <code>v0.2.1rc11</code> release.
== ngx.re.gmatch ==
'''syntax:''' ''iterator = ngx.re.gmatch(subject, regex, options?)''
'''syntax:''' ''iterator, err = ngx.re.gmatch(subject, regex, options?)''
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*''
Similar to [[#ngx.re.match|ngx.re.match]], but returns a Lua iterator instead, so as to let the user programmer iterate all the matches over the <code><subject></code> string argument with the PCRE <code>regex</code>.
In case of errors, like seeing an ill-formed regular expression, `nil` and a string describing the error will be returned.
Here is a small example to demonstrate its basic usage:
<geshi lang="lua">
local iterator = ngx.re.gmatch("hello, world!", "([a-z]+)", "i")
local iterator, err = ngx.re.gmatch("hello, world!", "([a-z]+)", "i")
if not iterator then
ngx.log(ngx.ERR, "error: ", err)
return
end
local m
m = iterator() -- m[0] == m[1] == "hello"
m = iterator() -- m[0] == m[1] == "world"
m = iterator() -- m == nil
m, err = iterator() -- m[0] == m[1] == "hello"
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
m, err = iterator() -- m[0] == m[1] == "world"
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
m, err = iterator() -- m == nil
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
</geshi>
More often we just put it into a Lua <code>for</code> loop:
More often we just put it into a Lua loop:
<geshi lang="lua">
for m in ngx.re.gmatch("hello, world!", "([a-z]+)", "i") do
local it, err = ngx.re.gmatch("hello, world!", "([a-z]+)", "i")
if not it then
ngx.log(ngx.ERR, "error: ", err)
return
end
while true do
local m, err = it()
if err then
ngx.log(ngx.ERR, "error: ", err)
return
end
if not m then
-- no match found (any more)
break
end
-- found a match
ngx.say(m[0])
ngx.say(m[1])
end
@ -3341,20 +3390,26 @@ This method requires the PCRE library enabled in Nginx. ([[#Special PCRE Sequen
This feature was first introduced in the <code>v0.2.1rc12</code> release.
== ngx.re.sub ==
'''syntax:''' ''newstr, n = ngx.re.sub(subject, regex, replace, options?)''
'''syntax:''' ''newstr, n, err = ngx.re.sub(subject, regex, replace, options?)''
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*''
Substitutes the first match of the Perl compatible regular expression <code>regex</code> on the <code>subject</code> argument string with the string or function argument <code>replace</code>. The optional <code>options</code> argument has exactly the same meaning as in [[#ngx.re.match|ngx.re.match]].
This method returns the resulting new string as well as the number of successful substitutions, or throw out a Lua exception when an error occurred (syntax errors in the <code><replace></code> string argument, for example).
This method returns the resulting new string as well as the number of successful substitutions. In case of failures, like syntax errors in the regular expressions or the <code><replace></code> string argument, it will return `nil` and a string describing the error.
When the <code>replace</code> is a string, then it is treated as a special template for string replacement. For example,
<geshi lang="lua">
local newstr, n = ngx.re.sub("hello, 1234", "([0-9])[0-9]", "[$0][$1]")
local newstr, n, err = ngx.re.sub("hello, 1234", "([0-9])[0-9]", "[$0][$1]")
if newstr then
-- newstr == "hello, [12][1]34"
-- n == 1
else
ngx.log(ngx.ERR, "error: ", err)
return
end
</geshi>
where <code>$0</code> referring to the whole substring matched by the pattern and <code>$1</code> referring to the first parenthesized capturing substring.
@ -3362,7 +3417,7 @@ where <code>$0</code> referring to the whole substring matched by the pattern an
Curly braces can also be used to disambiguate variable names from the background string literals:
<geshi lang="lua">
local newstr, n = ngx.re.sub("hello, 1234", "[0-9]", "${0}00")
local newstr, n, err = ngx.re.sub("hello, 1234", "[0-9]", "${0}00")
-- newstr == "hello, 10034"
-- n == 1
</geshi>
@ -3370,7 +3425,7 @@ Curly braces can also be used to disambiguate variable names from the background
Literal dollar sign characters (<code>$</code>) in the <code>replace</code> string argument can be escaped by another dollar sign, for instance,
<geshi lang="lua">
local newstr, n = ngx.re.sub("hello, 1234", "[0-9]", "$$")
local newstr, n, err = ngx.re.sub("hello, 1234", "[0-9]", "$$")
-- newstr == "hello, $234"
-- n == 1
</geshi>
@ -3383,7 +3438,7 @@ When the <code>replace</code> argument is of type "function", then it will be in
local func = function (m)
return "[" .. m[0] .. "][" .. m[1] .. "]"
end
local newstr, n = ngx.re.sub("hello, 1234", "( [0-9] ) [0-9]", func, "x")
local newstr, n, err = ngx.re.sub("hello, 1234", "( [0-9] ) [0-9]", func, "x")
-- newstr == "hello, [12][1]34"
-- n == 1
</geshi>
@ -3404,16 +3459,22 @@ Just like [[#ngx.re.sub|ngx.re.sub]], but does global substitution.
Here is some examples:
<geshi lang="lua">
local newstr, n = ngx.re.gsub("hello, world", "([a-z])[a-z]+", "[$0,$1]", "i")
local newstr, n, err = ngx.re.gsub("hello, world", "([a-z])[a-z]+", "[$0,$1]", "i")
if newstr then
-- newstr == "[hello,h], [world,w]"
-- n == 2
else
ngx.log(ngx.ERR, "error: ", err)
return
end
</geshi>
<geshi lang="lua">
local func = function (m)
return "[" .. m[0] .. "," .. m[1] .. "]"
end
local newstr, n = ngx.re.gsub("hello, world", "([a-z])[a-z]+", func, "i")
local newstr, n, err = ngx.re.gsub("hello, world", "([a-z])[a-z]+", func, "i")
-- newstr == "[hello,h], [world,w]"
-- n == 2
</geshi>

Просмотреть файл

@ -284,11 +284,13 @@ ngx_http_lua_ngx_re_match(lua_State *L)
if (rc != NGX_OK) {
dd("compile failed");
lua_pushnil(L);
re_comp.err.data[re_comp.err.len] = '\0';
msg = lua_pushfstring(L, "failed to compile regex \"%s\": %s",
pat.data, re_comp.err.data);
return luaL_argerror(L, 2, msg);
return 2;
}
#if (LUA_HAVE_PCRE_JIT)
@ -367,7 +369,7 @@ ngx_http_lua_ngx_re_match(lua_State *L)
if (cap == NULL) {
flags &= ~NGX_LUA_RE_COMPILE_ONCE;
msg = "out of memory";
msg = "no memory";
goto error;
}
@ -380,7 +382,8 @@ ngx_http_lua_ngx_re_match(lua_State *L)
re = ngx_palloc(pool, sizeof(ngx_http_lua_regex_t));
if (re == NULL) {
return luaL_error(L, "out of memory");
msg = "no memory";
goto error;
}
dd("saving regex %p, ncaptures %d, captures %p", re_comp.regex,
@ -537,7 +540,9 @@ error:
}
}
return luaL_error(L, msg);
lua_pushnil(L);
lua_pushstring(L, msg);
return 2;
}
@ -694,11 +699,13 @@ ngx_http_lua_ngx_re_gmatch(lua_State *L)
if (rc != NGX_OK) {
dd("compile failed");
lua_pushnil(L);
re_comp.err.data[re_comp.err.len] = '\0';
msg = lua_pushfstring(L, "failed to compile regex \"%s\": %s",
pat.data, re_comp.err.data);
return luaL_argerror(L, 2, msg);
return 2;
}
#if LUA_HAVE_PCRE_JIT
@ -775,7 +782,7 @@ ngx_http_lua_ngx_re_gmatch(lua_State *L)
cap = ngx_palloc(pool, ovecsize * sizeof(int));
if (cap == NULL) {
flags &= ~NGX_LUA_RE_COMPILE_ONCE;
msg = "out of memory";
msg = "no memory";
goto error;
}
@ -788,7 +795,8 @@ ngx_http_lua_ngx_re_gmatch(lua_State *L)
re = ngx_palloc(pool, sizeof(ngx_http_lua_regex_t));
if (re == NULL) {
return luaL_error(L, "out of memory");
msg = "no memory";
goto error;
}
dd("saving regex %p, ncaptures %d, captures %p", re_comp.regex,
@ -830,7 +838,8 @@ compiled:
cln = ngx_http_cleanup_add(r, 0);
if (cln == NULL) {
return luaL_error(L, "out of memory");
msg = "no memory";
goto error;
}
cln->handler = ngx_http_lua_ngx_re_gmatch_cleanup;
@ -863,7 +872,9 @@ error:
}
}
return luaL_error(L, msg);
lua_pushnil(L);
lua_pushstring(L, msg);
return 2;
}
@ -1051,7 +1062,9 @@ error:
ngx_pfree(r->pool, cap);
}
return luaL_error(L, msg);
lua_pushnil(L);
lua_pushstring(L, msg);
return 2;
}
@ -1356,11 +1369,14 @@ ngx_http_lua_ngx_re_sub_helper(lua_State *L, unsigned global)
if (rc != NGX_OK) {
dd("compile failed");
lua_pushnil(L);
lua_pushnil(L);
re_comp.err.data[re_comp.err.len] = '\0';
msg = lua_pushfstring(L, "failed to compile regex \"%s\": %s",
pat.data, re_comp.err.data);
return luaL_argerror(L, 2, msg);
return 3;
}
#if LUA_HAVE_PCRE_JIT
@ -1437,7 +1453,7 @@ ngx_http_lua_ngx_re_sub_helper(lua_State *L, unsigned global)
cap = ngx_palloc(pool, ovecsize * sizeof(int));
if (cap == NULL) {
flags &= ~NGX_LUA_RE_COMPILE_ONCE;
msg = "out of memory";
msg = "no memory";
goto error;
}
@ -1448,7 +1464,7 @@ ngx_http_lua_ngx_re_sub_helper(lua_State *L, unsigned global)
ctpl = ngx_palloc(pool, sizeof(ngx_http_lua_complex_value_t));
if (ctpl == NULL) {
flags &= ~NGX_LUA_RE_COMPILE_ONCE;
msg = "out of memory";
msg = "no memory";
goto error;
}
@ -1457,7 +1473,7 @@ ngx_http_lua_ngx_re_sub_helper(lua_State *L, unsigned global)
p = ngx_palloc(pool, tpl.len + 1);
if (p == NULL) {
flags &= ~NGX_LUA_RE_COMPILE_ONCE;
msg = "out of memory";
msg = "no memory";
goto error;
}
@ -1487,8 +1503,11 @@ ngx_http_lua_ngx_re_sub_helper(lua_State *L, unsigned global)
ngx_pfree(pool, re_comp.regex);
return luaL_error(L, "bad template for substitution: \"%s\"",
lua_tostring(L, 3));
lua_pushnil(L);
lua_pushnil(L);
lua_pushfstring(L, "bad template for substitution: \"%s\"",
lua_tostring(L, 3));
return 3;
}
}
@ -1501,7 +1520,8 @@ ngx_http_lua_ngx_re_sub_helper(lua_State *L, unsigned global)
re = ngx_palloc(pool, sizeof(ngx_http_lua_regex_t));
if (re == NULL) {
return luaL_error(L, "out of memory");
msg = "no memory";
goto error;
}
dd("saving regex %p, ncaptures %d, captures %p", re_comp.regex,
@ -1755,7 +1775,10 @@ error:
}
}
return luaL_error(L, msg);
lua_pushnil(L);
lua_pushnil(L);
lua_pushstring(L, msg);
return 3;
}

Просмотреть файл

@ -9,7 +9,7 @@ use Test::Nginx::Socket;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 2 + 8);
plan tests => repeat_each() * (blocks() * 2 + 10);
#no_diff();
no_long_string();
@ -345,22 +345,26 @@ he
--- config
location /re {
content_by_lua '
rc, m = pcall(ngx.re.match, "hello\\nworld", "(abc")
if rc then
if m then
ngx.say(m[0])
local m, err = ngx.re.match("hello\\nworld", "(abc")
if m then
ngx.say(m[0])
else
if err then
ngx.say("error: ", err)
else
ngx.say("not matched: ", m)
end
else
ngx.say("error: ", m)
end
';
}
--- request
GET /re
--- response_body
error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]
@ -628,20 +632,27 @@ regex: (?:>[\w\s]*</?\w{2,}>)
--- config
location /re {
content_by_lua '
m = ngx.re.match("hello, 1234", "([0-9]+")
local m, err = ngx.re.match("hello, 1234", "([0-9]+")
if m then
ngx.say(m[0])
else
ngx.say("not matched!")
if err then
ngx.say("error: ", err)
else
ngx.say("not matched!")
end
end
';
}
--- request
GET /re
--- response_body_like: 500 Internal Server Error
--- error_code: 500
--- error_log chop
lua entry thread aborted: runtime error: [string "content_by_lua"]:2: bad argument #2 to 'match' (failed to compile regex "([0-9]+": pcre_compile() failed: missing ) in "([0-9]+")
--- response_body
error: failed to compile regex "([0-9]+": pcre_compile() failed: missing ) in "([0-9]+"
--- no_error_log
[error]
@ -902,3 +913,35 @@ nil
--- no_error_log
[error]
=== TEST 42: bad UTF-8
--- config
location = /t {
content_by_lua '
local target = "你好"
local regex = "你好"
-- Note the D here
local m, err = ngx.re.match(string.sub(target, 1, 4), regex, "u")
if err then
ngx.say("error: ", err)
return
end
if m then
ngx.say("matched: ", m[0])
else
ngx.say("not matched")
end
';
}
--- request
GET /t
--- response_body_like chop
error: pcre_exec\(\) failed: -10 on "你.*?" using "你好"
--- no_error_log
[error]

Просмотреть файл

@ -9,7 +9,7 @@ use Test::Nginx::Socket;
repeat_each(5);
plan tests => repeat_each() * (blocks() * 2 + 1);
plan tests => repeat_each() * (blocks() * 2 + 3);
our $HtmlDir = html_dir;
@ -681,3 +681,63 @@ not matched!
1234
=== TEST 26: bad pattern
--- config
location /re {
content_by_lua '
local it, err = ngx.re.gmatch("hello\\nworld", "(abc")
if not err then
ngx.say("good")
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]
=== TEST 27: bad UTF-8
--- config
location = /t {
content_by_lua '
local target = "你好"
local regex = "你好"
-- Note the D here
local it, err = ngx.re.gmatch(string.sub(target, 1, 4), regex, "u")
if err then
ngx.say("error: ", err)
return
end
local m, err = it()
if err then
ngx.say("error: ", err)
return
end
if m then
ngx.say("matched: ", m[0])
else
ngx.say("not matched")
end
';
}
--- request
GET /t
--- response_body_like chop
error: pcre_exec\(\) failed: -10 on "你.*?"
--- no_error_log
[error]

Просмотреть файл

@ -9,7 +9,7 @@ use Test::Nginx::Socket;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 2 + 1);
plan tests => repeat_each() * (blocks() * 2 + 9);
#no_diff();
no_long_string();
@ -72,19 +72,22 @@ a [b c] [b] [c] [] [] d
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "a b c d",
local s, n, err = ngx.re.sub("a b c d",
"(b) (c)", "[$0] [$1] [$2] [$3] [$hello]")
ngx.say(rc)
ngx.say(s)
ngx.say(n)
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false
bad template for substitution: "[$0] [$1] [$2] [$3] [$hello]"
nil
error: bad template for substitution: "[$0] [$1] [$2] [$3] [$hello]"
--- error_log
attempt to use named capturing variable "hello" (named captures not supported yet)
@ -92,19 +95,21 @@ nil
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "a b c d",
local s, n, err = ngx.re.sub("a b c d",
"(b) (c)", "[$0] [$1] [$2] [$3] [${hello}]")
ngx.say(rc)
ngx.say(s)
ngx.say(n)
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false
bad template for substitution: "[$0] [$1] [$2] [$3] [${hello}]"
nil
error: bad template for substitution: "[$0] [$1] [$2] [$3] [${hello}]"
--- error_log
attempt to use named capturing variable "hello" (named captures not supported yet)
@ -129,18 +134,20 @@ nil
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${134]")
ngx.say(rc)
ngx.say(s)
ngx.say(n)
local s, n, err = ngx.re.sub("b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${134]")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false
bad template for substitution: "[$0] [$1] [${2}] [$3] [${134]"
nil
error: bad template for substitution: "[$0] [$1] [${2}] [$3] [${134]"
--- error_log
the closing bracket in "134" variable is missing
@ -148,18 +155,20 @@ nil
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${134")
ngx.say(rc)
ngx.say(s)
ngx.say(n)
local s, n, err = ngx.re.sub("b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${134")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false
bad template for substitution: "[$0] [$1] [${2}] [$3] [${134"
nil
error: bad template for substitution: "[$0] [$1] [${2}] [$3] [${134"
--- error_log
the closing bracket in "134" variable is missing
@ -167,18 +176,20 @@ nil
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${")
ngx.say(rc)
ngx.say(s)
ngx.say(n)
local s, n, err = ngx.re.sub("b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false
bad template for substitution: "[$0] [$1] [${2}] [$3] [${"
nil
error: bad template for substitution: "[$0] [$1] [${2}] [$3] [${"
--- error_log
lua script: invalid capturing variable name found in "[$0] [$1] [${2}] [$3] [${"
@ -186,18 +197,20 @@ nil
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [$")
ngx.say(rc)
ngx.say(s)
ngx.say(n)
local s, n, err = ngx.re.sub("b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [$")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false
bad template for substitution: "[$0] [$1] [${2}] [$3] [$"
nil
error: bad template for substitution: "[$0] [$1] [${2}] [$3] [$"
--- error_log
lua script: invalid capturing variable name found in "[$0] [$1] [${2}] [$3] [$"
@ -445,3 +458,52 @@ a [b c] [b] [c] [] [] d
--- no_error_log
[error]
=== TEST 24: bad pattern
--- config
location /re {
content_by_lua '
local s, n, err = ngx.re.sub("hello\\nworld", "(abc", "")
if s then
ngx.say("subs: ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]
=== TEST 25: bad UTF-8
--- config
location = /t {
content_by_lua '
local target = "你好"
local regex = "你好"
-- Note the D here
local s, n, err = ngx.re.sub(string.sub(target, 1, 4), regex, "", "u")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /t
--- response_body_like chop
error: pcre_exec\(\) failed: -10 on "你.*?" using "你好"
--- no_error_log
[error]

Просмотреть файл

@ -9,7 +9,7 @@ log_level('warn');
repeat_each(2);
plan tests => repeat_each() * (blocks() * 2 + 9);
plan tests => repeat_each() * (blocks() * 2 + 10);
#no_diff();
no_long_string();
@ -403,3 +403,30 @@ n: 1
--- no_error_log
[error]
=== TEST 20: bad UTF-8
--- config
location = /t {
content_by_lua '
local target = "你好"
local regex = "你好"
-- Note the D here
local s, n, err = ngx.re.gsub(string.sub(target, 1, 4), regex, "", "u")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /t
--- response_body_like chop
error: pcre_exec\(\) failed: -10 on "你.*?" using "你好"
--- no_error_log
[error]

Просмотреть файл

@ -9,7 +9,7 @@ log_level('warn');
repeat_each(2);
plan tests => repeat_each() * (blocks() * 2 + 2);
plan tests => repeat_each() * (blocks() * 2 + 3);
#no_diff();
#no_long_string();
@ -321,22 +321,26 @@ he
--- config
location /re {
content_by_lua '
rc, m = pcall(ngx.re.match, "hello\\nworld", "(abc", "o")
if rc then
if m then
ngx.say(m[0])
local m, err = ngx.re.match("hello\\nworld", "(abc", "o")
if m then
ngx.say(m[0])
else
if err then
ngx.say("error: ", err)
else
ngx.say("not matched: ", m)
end
else
ngx.say("error: ", m)
end
';
}
--- request
GET /re
--- response_body
error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]

Просмотреть файл

@ -9,7 +9,7 @@ use Test::Nginx::Socket;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 2);
plan tests => repeat_each() * (blocks() * 2 + 6);
#no_diff();
no_long_string();
@ -68,23 +68,28 @@ a [b c] [b] [c] [] [] d
=== TEST 4: matched and with named variables
=== TEST 4: matched and with named variables (bad template)
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "a b c d",
"(b) (c)", "[$0] [$1] [$2] [$3] [$hello]", "o")
ngx.say(rc)
ngx.say(s)
ngx.say(n)
local s, n, err = ngx.re.sub("a b c d",
"(b) (c)",
"[$0] [$1] [$2] [$3] [$hello]",
"o")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false
bad template for substitution: "[$0] [$1] [$2] [$3] [$hello]"
nil
error: bad template for substitution: "[$0] [$1] [$2] [$3] [$hello]"
--- error_log
attempt to use named capturing variable "hello" (named captures not supported yet)
@ -92,19 +97,23 @@ nil
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "a b c d",
"(b) (c)", "[$0] [$1] [$2] [$3] [${hello}]", "o")
ngx.say(rc)
ngx.say(s)
ngx.say(n)
local s, n, err = ngx.re.sub("a b c d",
"(b) (c)",
"[$0] [$1] [$2] [$3] [${hello}]",
"o")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false
bad template for substitution: "[$0] [$1] [$2] [$3] [${hello}]"
nil
error: bad template for substitution: "[$0] [$1] [$2] [$3] [${hello}]"
--- error_log
attempt to use named capturing variable "hello" (named captures not supported yet)
@ -129,18 +138,20 @@ nil
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${134]", "o")
ngx.say(rc)
ngx.say(s)
ngx.say(n)
local s, n, err = ngx.re.sub("b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${134]", "o")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false
bad template for substitution: "[$0] [$1] [${2}] [$3] [${134]"
nil
error: bad template for substitution: "[$0] [$1] [${2}] [$3] [${134]"
--- error_log
the closing bracket in "134" variable is missing
@ -148,18 +159,20 @@ nil
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${134", "o")
ngx.say(rc)
ngx.say(s)
ngx.say(n)
local s, n, err = ngx.re.sub("b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${134", "o")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false
bad template for substitution: "[$0] [$1] [${2}] [$3] [${134"
nil
error: bad template for substitution: "[$0] [$1] [${2}] [$3] [${134"
--- error_log
the closing bracket in "134" variable is missing
@ -167,18 +180,20 @@ nil
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${", "o")
ngx.say(rc)
ngx.say(s)
ngx.say(n)
local s, n, err = ngx.re.sub("b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${", "o")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false
bad template for substitution: "[$0] [$1] [${2}] [$3] [${"
nil
error: bad template for substitution: "[$0] [$1] [${2}] [$3] [${"
--- error_log
lua script: invalid capturing variable name found in "[$0] [$1] [${2}] [$3] [${"
@ -186,18 +201,20 @@ nil
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [$", "o")
ngx.say(rc)
ngx.say(s)
ngx.say(n)
local s, n, err = ngx.re.sub("b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [$", "o")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false
bad template for substitution: "[$0] [$1] [${2}] [$3] [$"
nil
error: bad template for substitution: "[$0] [$1] [${2}] [$3] [$"
--- error_log
lua script: invalid capturing variable name found in "[$0] [$1] [${2}] [$3] [$"

Просмотреть файл

@ -9,7 +9,7 @@ use Test::Nginx::Socket;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 2 + 4);
plan tests => repeat_each() * (blocks() * 2 + 5);
#no_diff();
no_long_string();
@ -105,20 +105,24 @@ pcre JIT compiling result: 1
--- config
location /re {
content_by_lua '
rc, m = pcall(ngx.re.match, "hello\\nworld", "(abc", "j")
if rc then
if m then
ngx.say(m[0])
local m, err = ngx.re.match("hello\\nworld", "(abc", "j")
if m then
ngx.say(m[0])
else
if err then
ngx.say("error: ", err)
else
ngx.say("not matched: ", m)
end
else
ngx.say("error: ", m)
end
';
}
--- request
GET /re
--- response_body
error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]

Просмотреть файл

@ -9,7 +9,7 @@ use Test::Nginx::Socket;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 2 + 8);
plan tests => repeat_each() * (blocks() * 2 + 9);
#no_diff();
#no_long_string();
@ -197,8 +197,8 @@ pcre JIT compiling result: 1
--- config
location /re {
content_by_lua '
rc, err = pcall(ngx.re.gmatch, "hello\\nworld", "(abc", "j")
if not rc then
local m, err = ngx.re.gmatch("hello\\nworld", "(abc", "j")
if not m then
ngx.say("error: ", err)
return
end
@ -208,5 +208,7 @@ pcre JIT compiling result: 1
--- request
GET /re
--- response_body
error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]

Просмотреть файл

@ -9,7 +9,7 @@ use Test::Nginx::Socket;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 2);
plan tests => repeat_each() * (blocks() * 2 + 1);
#no_diff();
#no_long_string();
@ -202,8 +202,8 @@ hello
--- config
location /re {
content_by_lua '
rc, err = pcall(ngx.re.gmatch, "hello\\nworld", "(abc", "d")
if not rc then
local it, err = ngx.re.gmatch("hello\\nworld", "(abc", "d")
if not it then
ngx.say("error: ", err)
return
end
@ -213,5 +213,7 @@ hello
--- request
GET /re
--- response_body
error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]

Просмотреть файл

@ -9,7 +9,7 @@ use Test::Nginx::Socket;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 2 + 4);
plan tests => repeat_each() * (blocks() * 2 + 6);
#no_diff();
no_long_string();
@ -105,18 +105,20 @@ pcre JIT compiling result: 1
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "hello\\nworld", "(abc", "world", "j")
if rc then
local s, n, err = ngx.re.sub("hello\\nworld", "(abc", "world", "j")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", s)
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]
@ -124,16 +126,18 @@ error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() fa
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.sub, "hello\\nworld", "(abc", "world", "jo")
if rc then
local s, n, err = ngx.re.sub( "hello\\nworld", "(abc", "world", "jo")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", s)
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]

Просмотреть файл

@ -9,7 +9,7 @@ use Test::Nginx::Socket;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 2);
plan tests => repeat_each() * (blocks() * 2 + 2);
#no_diff();
no_long_string();
@ -97,14 +97,21 @@ hello, world: 0
--- config
location /re {
content_by_lua '
rc, m = pcall(ngx.re.sub, "hello\\nworld", "(abc", "world", "j")
ngx.say(rc, ": ", m)
local s, n, err = ngx.re.sub("hello\\nworld", "(abc", "world", "j")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]
@ -112,12 +119,19 @@ false: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() fa
--- config
location /re {
content_by_lua '
rc, m = pcall(ngx.re.sub, "hello\\nworld", "(abc", "world", "jo")
ngx.say(rc, ": ", m)
local s, n, err = ngx.re.sub("hello\\nworld", "(abc", "world", "jo")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]

Просмотреть файл

@ -9,7 +9,7 @@ use Test::Nginx::Socket;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 2 + 4);
plan tests => repeat_each() * (blocks() * 2 + 6);
#no_diff();
no_long_string();
@ -105,18 +105,20 @@ pcre JIT compiling result: 1
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.gsub, "hello\\nworld", "(abc", "world", "j")
if rc then
local s, n, err = ngx.re.gsub("hello\\nworld", "(abc", "world", "j")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", s)
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]
@ -124,16 +126,18 @@ error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() fa
--- config
location /re {
content_by_lua '
local rc, s, n = pcall(ngx.re.gsub, "hello\\nworld", "(abc", "world", "jo")
if rc then
local s, n, err = ngx.re.gsub("hello\\nworld", "(abc", "world", "jo")
if s then
ngx.say(s, ": ", n)
else
ngx.say("error: ", s)
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
error: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]

Просмотреть файл

@ -9,7 +9,7 @@ use Test::Nginx::Socket;
repeat_each(2);
plan tests => repeat_each() * (blocks() * 2);
plan tests => repeat_each() * (blocks() * 2 + 1);
#no_diff();
no_long_string();
@ -97,14 +97,19 @@ hello, world: 0
--- config
location /re {
content_by_lua '
rc, m = pcall(ngx.re.gsub, "hello\\nworld", "(abc", "world", "j")
ngx.say(rc, ": ", m)
local s, n, err = ngx.re.gsub("hello\\nworld", "(abc", "world", "j")
if s then
ngx.say("gsub: ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
@ -112,12 +117,18 @@ false: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() fa
--- config
location /re {
content_by_lua '
rc, m = pcall(ngx.re.gsub, "hello\\nworld", "(abc", "world", "jo")
ngx.say(rc, ": ", m)
local s, n, err = ngx.re.gsub("hello\\nworld", "(abc", "world", "jo")
if s then
ngx.say("gsub: ", n)
else
ngx.say("error: ", err)
end
';
}
--- request
GET /re
--- response_body
false: bad argument #2 to '?' (failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc")
error: failed to compile regex "(abc": pcre_compile() failed: missing ) in "(abc"
--- no_error_log
[error]