## **正则测试用例:**
~~~
--re. find , re. gsub , re. match , re. gmatch 可用。这些函数具有与 Lua 模式匹配相同的 API
-- 重定义assert函数
local assert = function(a, b)
if a == true then
print('true')
return
end
print(a)
if b then
print(b)
end
end
local re = woo.re
local a, b, i, s, t
-- quote
assert(re.quote("^$.?a")) --== [[\^\$\.\?a]]
-- find
function f(s, p)
local i, e = string.find(s, p)
if i then
return string.sub(s, i, e)
end
end
a, b = re.find('', '')
assert(a, b)
a, b = re.find("abcd efgh ijk", "cd e", 1, true)
assert(a, b)
a, b = re.find("abcd efgh ijk", "cd e", -1, true)
assert(a)
assert(not pcall(re.find, "aaa", "(aaaa"))
assert(re.find("aaaa", "(b|c)*"))
a, b, s = re.find("abcd efgh ijk", "i([jk])")
assert('abs:' .. a .. '+' .. b .. '+' .. s)
-- gsub
assert(not pcall(re.gsub, "aaa", "(aaaa", "${1}"))
s, a = re.gsub("aaaa", "bbbb", "")
assert(s, a)
assert(re.gsub("hello world", [[(\w+)]], "${1} ${1}"))
t = { name = "lua", version = "5.1" }
assert(re.gsub("$name-$version.tar.gz", [[\$(\w+)]], t))
assert(re.gsub("name version", [[\w+]], t))
assert(re.gsub("4+5 = $return 4+5$", "\\$(.*)\\$", function(s)
return loadstring(s)()
end) == "4+5 = 9")
assert(re.gsub("$ world", "\\w+", string.upper))
-- gmatch
assert(not pcall(re.gmatch, "hello world", "(aaaaa"))
i = 1
for w in re.gmatch("hello world", "\\w+") do
if i == 1 then
assert('gmatch1:' .. w)
elseif i == 2 then
assert('gmatch2' .. w)
end
i = i + 1
end
assert('final i=' .. i)
i = 1
for k, v in re.gmatch("from=world, to=Lua", "(\\w+)=(\\w+)") do
if i == 1 then
assert(k == "from" and v == "world")
elseif i == 2 then
assert(k == "to" and v == "Lua")
end
i = i + 1
end
assert(i == 3)
-- match
assert(not pcall(re.match, "hello world", "(aaaaa"))
assert(re.match("$$$ hello", "z") == nil)
assert(re.match("$$$ hello", "\\w+") == "hello")
assert(re.match("hello world", "\\w+", 6) == "world")
assert(re.match("hello world", "\\w+", -5) == "world")
a, b = re.match("from=world", "(\\w+)=(\\w+)")
assert(a == "from" and b == "world")
for k, v in woo.re.gmatch("from=world, to=Lua", "(\\w+)=(\\w+)") do
print(k,v)
end
~~~
## **输出:**
\^\$\.\?a
1
0
3
6
nil
true
nil
abs:11+12+j
true
aaaa
0
hello hello world world
2
lua-5.1.tar.gz
2
lua 5.1
2
true
$ WORLD
1
true
gmatch1:hello
gmatch2world
final i=3
true
true
true
true
true
true
true
true
true
from world
to Lua
- 序言
- 安装
- 可视化编辑器
- woo 命令行参数详解
- 测试用例
- 简单http服务
- 正则
- gpio控制硬件设备
- 比武不招亲
- 和php比WebServer
- woo语法
- 语法简介
- 基本语法
- woo数据类型
- woo变量
- woo循环
- woo流程控制
- woo函数
- woo运算符
- woo字符串
- 类
- 类的继承
- 模块
- 数组
- 迭代
- table
- 元表(魔术方法)
- 错误处理
- 面向对象
- woo开发必须注意事项
- 函数/模块
- 内置函数
- webServer专属函数
- 内置模块
- gd-2d绘图库
- 2D图形例子
- websockets用例
- buffer缓存
- http
- orm数据库连接
- redis-ssdb连接
- queue队列
- 其他特别函数
- sockets
- 启动线程如何
- Murphy-http Web框架
- 简介
- 全局函数
- model
- view
- controller
- 消息队列
- 全局线程锁
- 包管理器/coder wooyri package manager
- 最ok的包上榜名单
- woo栗子
- 函数传参
- 编码解码
- http模块用例
- 移动读写文件流用例
- 文件下载用例
- 数组迭代
- 进制转换
- _choose用例
- 日期时间用例
- 注明
- wop 码包|coder wooyri peogram
- 码人激励计划