You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
locals="hello, 1234"
4137
+
localfrom, to, err=ngx.re.find(s, "([0-9]+)", "jo")
4138
+
iffromthen
4139
+
ngx.say("from: ", from)
4140
+
ngx.say("to: ", to)
4141
+
ngx.say("matched: ", string.sub(s, from, to))
4142
+
else
4143
+
iferrthen
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.
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.
0 commit comments