Skip to content

Commit 2d6cf5c

Browse files
committed
documented the new ngx.re.find API function.
1 parent 87dd3cf commit 2d6cf5c

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

README

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3865,6 +3865,52 @@ Nginx API for Lua
38653865

38663866
This feature was introduced in the "v0.2.1rc11" release.
38673867

3868+
ngx.re.find
3869+
syntax: *from, to, err = ngx.re.find(subject, regex, options?, ctx?)*
3870+
3871+
context: *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*,
3872+
header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.**
3873+
3874+
Similar to ngx.re.match but only returns the begining index ("from") and
3875+
end index ("to") of the matched substring. The returned indexes are
3876+
1-based and can be fed directly into the string.sub
3877+
(<http://www.lua.org/manual/5.1/manual.html#pdf-string.sub>) API
3878+
function to obtain the matched substring.
3879+
3880+
In case of errors (like bad regexes or any PCRE runtime errors), this
3881+
API function returns two "nil" values followed by a string describing
3882+
the error.
3883+
3884+
If no match is found, this function just returns "nil" values.
3885+
3886+
Below is an example:
3887+
3888+
local s = "hello, 1234"
3889+
local from, to, err = ngx.re.find(s, "([0-9]+)", "jo")
3890+
if from then
3891+
ngx.say("from: ", from)
3892+
ngx.say("to: ", to)
3893+
ngx.say("matched: ", string.sub(s, from, to))
3894+
else
3895+
if err then
3896+
ngx.say("error: ", err)
3897+
return
3898+
end
3899+
ngx.say("not matched!")
3900+
end
3901+
3902+
This example produces the output
3903+
3904+
from: 8
3905+
to: 11
3906+
matched: 1234
3907+
3908+
Because this API function does not create new Lua strings nor new Lua
3909+
tables, it is much faster than ngx.re.match. It should be used wherever
3910+
possible.
3911+
3912+
This API function was first introduced in the "v0.9.2" release.
3913+
38683914
ngx.re.gmatch
38693915
syntax: *iterator, err = ngx.re.gmatch(subject, regex, options?)*
38703916

README.markdown

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ Table of Contents
127127
* [ngx.parse_http_time](#ngxparse_http_time)
128128
* [ngx.is_subrequest](#ngxis_subrequest)
129129
* [ngx.re.match](#ngxrematch)
130+
* [ngx.re.find](#ngxrefind)
130131
* [ngx.re.gmatch](#ngxregmatch)
131132
* [ngx.re.sub](#ngxresub)
132133
* [ngx.re.gsub](#ngxregsub)
@@ -4116,6 +4117,49 @@ This feature was introduced in the `v0.2.1rc11` release.
41164117

41174118
[Back to TOC](#table-of-contents)
41184119

4120+
ngx.re.find
4121+
-----------
4122+
**syntax:** *from, to, err = ngx.re.find(subject, regex, options?, ctx?)*
4123+
4124+
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.**
4125+
4126+
Similar to [ngx.re.match](#ngxrematch) but only returns the begining index (`from`) and end index (`to`) of the matched substring. The returned indexes are 1-based and can be fed directly into the [string.sub](http://www.lua.org/manual/5.1/manual.html#pdf-string.sub) API function to obtain the matched substring.
4127+
4128+
In case of errors (like bad regexes or any PCRE runtime errors), this API function returns two `nil` values followed by a string describing the error.
4129+
4130+
If no match is found, this function just returns `nil` values.
4131+
4132+
Below is an example:
4133+
4134+
```lua
4135+
4136+
local s = "hello, 1234"
4137+
local from, to, err = ngx.re.find(s, "([0-9]+)", "jo")
4138+
if from then
4139+
ngx.say("from: ", from)
4140+
ngx.say("to: ", to)
4141+
ngx.say("matched: ", string.sub(s, from, to))
4142+
else
4143+
if err then
4144+
ngx.say("error: ", err)
4145+
return
4146+
end
4147+
ngx.say("not matched!")
4148+
end
4149+
```
4150+
4151+
This example produces the output
4152+
4153+
from: 8
4154+
to: 11
4155+
matched: 1234
4156+
4157+
Because this API function does not create new Lua strings nor new Lua tables, it is much faster than [ngx.re.match](#ngxrematch). It should be used wherever possible.
4158+
4159+
This API function was first introduced in the `v0.9.2` release.
4160+
4161+
[Back to TOC](#table-of-contents)
4162+
41194163
ngx.re.gmatch
41204164
-------------
41214165
**syntax:** *iterator, err = ngx.re.gmatch(subject, regex, options?)*

doc/HttpLuaModule.wiki

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3427,6 +3427,45 @@ To confirm that PCRE JIT is enabled, activate the Nginx debug log by adding the
34273427
34283428
This feature was introduced in the <code>v0.2.1rc11</code> release.
34293429
3430+
== ngx.re.find ==
3431+
'''syntax:''' ''from, to, err = ngx.re.find(subject, regex, options?, ctx?)''
3432+
3433+
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*''
3434+
3435+
Similar to [[#ngx.re.match|ngx.re.match]] but only returns the begining index (<code>from</code>) and end index (<code>to</code>) of the matched substring. The returned indexes are 1-based and can be fed directly into the [http://www.lua.org/manual/5.1/manual.html#pdf-string.sub string.sub] API function to obtain the matched substring.
3436+
3437+
In case of errors (like bad regexes or any PCRE runtime errors), this API function returns two <code>nil</code> values followed by a string describing the error.
3438+
3439+
If no match is found, this function just returns <code>nil</code> values.
3440+
3441+
Below is an example:
3442+
3443+
<geshi lang="lua">
3444+
local s = "hello, 1234"
3445+
local from, to, err = ngx.re.find(s, "([0-9]+)", "jo")
3446+
if from then
3447+
ngx.say("from: ", from)
3448+
ngx.say("to: ", to)
3449+
ngx.say("matched: ", string.sub(s, from, to))
3450+
else
3451+
if err then
3452+
ngx.say("error: ", err)
3453+
return
3454+
end
3455+
ngx.say("not matched!")
3456+
end
3457+
</geshi>
3458+
3459+
This example produces the output
3460+
3461+
from: 8
3462+
to: 11
3463+
matched: 1234
3464+
3465+
Because this API function does not create new Lua strings nor new Lua tables, it is much faster than [[#ngx.re.match|ngx.re.match]]. It should be used wherever possible.
3466+
3467+
This API function was first introduced in the <code>v0.9.2</code> release.
3468+
34303469
== ngx.re.gmatch ==
34313470
'''syntax:''' ''iterator, err = ngx.re.gmatch(subject, regex, options?)''
34323471

0 commit comments

Comments
 (0)