正則表達式相關函數
更新時間 2024-07-22 09:45:33
最近更新時間: 2024-07-22 09:45:33
分享文章
本文簡述正則表達式相關函數的語法、作用、入參、返回值、示例。
ctyun.re.match
函數信息詳見下表:
| 項目 | 描述 |
|---|---|
| 語法 | captures_table, err = ctyun.re.match(subject, regex, options?) |
| 作用 | 正則匹配,返回匹配內容。 |
| 入參 | subject:待匹配字符串。 regex:正則表達式。 options:控制如何執行匹配操作,支持以下選項: a anchored mode (only match from the beginning) d enable the DFA mode (or the longest token match semantics). this requires PCRE 6.0+ or else a Lua exception will be thrown. first introduced in ngx_lua v0.3.1rc30. D enable duplicate named pattern support. This allows named subpattern names to be repeated, returning the captures in an array-like Lua table. for example, local m = ngx.re.match("hello, world", "(?\w+), (?\w+)", "D") -- m["named"] == {"hello", "world"} this option was first introduced in the v0.7.14 release. this option requires at least PCRE 8.12. i case insensitive mode (similar to Perl's /i modifier) j enable PCRE JIT compilation, this requires PCRE 8.21+ which must be built with the --enable-jit option. for optimum performance, this option should always be used together with the 'o' option. first introduced in ngx_lua v0.3.1rc30. J enable the PCRE Javascript compatible mode. this option was first introduced in the v0.7.14 release. this option requires at least PCRE 8.12. m multi-line mode (similar to Perl's /m modifier) o compile-once mode (similar to Perl's /o modifier), to enable the worker-process-level compiled-regex cache s single-line mode (similar to Perl's /s modifier) u UTF-8 mode. this requires PCRE to be built with the --enable-utf8 option or else a Lua exception will be thrown. U similar to "u" but disables PCRE's UTF-8 validity check on the subject string. first introduced in ngx_lua v0.8.1. x extended mode (similar to Perl's /x modifier) |
| 返回值 | captures_table:找到匹配項時,返回一個lua table,其中captures_table[0]保存整個匹配的子字符串,captures_table[1]保存第一個帶括號的子模式的捕獲,captures_table[2]為第二個,以此類推。未找到匹配項時此項為nil。 err:描述錯誤信息的字符串。 |
示例:
local m, err = ctyun.re.match("hello, 1234", "([0-9])[0-9]+", 'jo')
-- m[0] == "1234"
-- m[1] == "1"
-- 命名捕獲
local m, err = ctyun.re.match("hello, 1234", "([0-9])(?<remaining>[0-9]+)")
-- m[0] == "1234"
-- m[1] == "1"
-- m[2] == "234"
-- m["remaining"] == "234"
ctyun.re.find
函數信息詳見下表:
| 項目 | 描述 |
|---|---|
| 語法 | from, to, err = ctyun.re.find(subject, regex, options?) |
| 作用 | 正則匹配,返回匹配子字符串的開始索引 ( from) 和結束索引 ( to)。 |
| 入參 | subject:待匹配字符串。 regex:正則表達式。 options:控制如何執行匹配操作。 |
| 返回值 | from:匹配子字符串的開始索引,未找到匹配項時值為nil。 to:匹配子字符串的結束索引。 err:描述錯誤信息的字符串。 |
示例:
local s = "hello, 1234"
local from, to, err = ctyun.re.find(s, "([0-9]+)", "jo")
if from then
-- from == 8
-- to == 11
local matched = string.sub(s, from, to)
-- matched == "1234"
else
if err then
return
end
end
ctyun.re.gmatch
函數信息詳見下表:
| 項目 | 描述 |
|---|---|
| 語法 | iterator, err = ctyun.re.gmatch(subject, regex, options?) |
| 作用 | 類似于ctyun.re.match,但返回一個 Lua 迭代器。 |
| 入參 | subject:待匹配字符串。 regex:正則表達式。 options:控制如何執行匹配操作。 |
| 返回值 | iterator: Lua 迭代器,可遍歷獲取所有匹配項。 err:描述錯誤信息的字符串。 |
示例:
local iterator, err = ctyun.re.gmatch("hello, world!", "([a-z]+)", "i")
if not iterator then
return
end
local m
m, err = iterator() -- m[0] == m[1] == "hello"
if err then
return
end
m, err = iterator() -- m[0] == m[1] == "world"
if err then
return
end
m, err = iterator() -- m == nil
if err then
return
end
ctyun.re.sub
函數信息詳見下表:
| 項目 | 描述 |
|---|---|
| 語法 | new, n, err = ctyun.re.sub(subject, regex, replace, options?) |
| 作用 | 該方法主要實現匹配字符串的替換,會用replace替換匹配的字串,replace可以是純字符串,也可以是使用$0, $1等子模式串的形式,ctyun.re.sub返回進行替換后的完整的字符串,同時返回替換的總個數。 |
| 入參 | subject:待匹配字符串。 regex:正則表達式。 replace:替換匹配的字符串,可以是純字符串,也可以使用$0$1捕獲匹配,也可兩者結合來使用。 options:控制如何執行匹配操作。 |
| 返回值 | new:替換生成的新字符串。 n:成功替換的次數。 err:描述錯誤信息的字符串。 |
示例:
local newstr, n, err = ctyun.re.sub("hello, 1234", "[0-9]", "${0}00")
-- newstr == "hello, 100234"
-- n == 1
ctyun.re.gsub
函數信息詳見下表:
| 項目 | 描述 |
|---|---|
| 語法 | new, n, err = ctyun.re.gsub(subject, regex, replace, options?) |
| 作用 | 類似ctyun.re.gsub,但是會進行全局替換。 |
| 入參 | subject:待匹配字符串。 regex:正則表達式。 replace:替換匹配的字符串,可以是純字符串,也可以使用$0$1捕獲匹配,也可兩者結合來使用。 options:控制如何執行匹配操作。 |
| 返回值 | new:替換生成的新字符串。 n:成功替換的次數。 err:描述錯誤信息的字符串。 |
示例:
local newstr, n, err = ctyun.re.gsub("hello, world", "([a-z])[a-z]+", "[$0,$1]", "i")
if not newstr then
return
end
-- newstr == "[hello,h], [world,w]"
-- n == 2