如何在Lua中使用正则表达式重复匹配多个表达式
2014-1-19 11:12:4
收藏:0
阅读:98
评论:4
我正在学习 lua ,在 regexp 中有一些问题。
我有一些字符串:
text = "aaab1aaac-aac1d2b5hhpt456d5h9h8"
我想得到的结果是:
"b1", "c1b2b5", "t4", "d5h9h8"
我编写了以下代码:
local st,ed
while true do
st,ed = string.find(text,"([a-z][1-9])+",ed) --the regexp
if st==nil then
break
else
print(string.sub(text,st,ed))
end
ed=ed+1
end
但它并没有输出正确的结果。
点赞
用户441830
正如@Yu Hao在评论中提到的那样,Lua模式与我们大多数人使用的“正则表达式”不同且有些不那么强大。但这实际上并不是问题,因为Lua提供了由语言的主要开发者之一编写的优秀的LPEG库。
你正在请求的模式可以在LPEG中编写如下:
local lpeg = require "lpeg"
local lpegmatch = lpeg.match
local R, C = lpeg.R, lpeg.C
local match_alpha_n_digit
do
local alpha = R "az" -- + R "AZ" -- for uppercase
local digit = R "09"
local sequence = C ((alpha * digit)^1) -- capture longest sequence of (alpha, digit) pairs
local pattern = (sequence + 1)^1
match_alpha_n_digit = function (str)
if not str or type (str) ~= "string" then return end
return lpegmatch (pattern, str)
end
end
text = "aaab1aaac-aac1d2b5hhpt456d5h9h8"
print (match_alpha_n_digit (text))
--- or capture the result in a table:
some_table = { match_alpha_n_digit (text) }
这种方式是将其包装在match_alpha_n_digit()函数中,您可以在表构造器内调用该函数。
还可以编写接收任意额外参数的模式,然后在匹配时使用参数捕获(lpeg.Carg())检索这些参数。该方法允许例如使用函数迭代所有匹配:
local lpeg = require "lpeg"
local lpegmatch = lpeg.match
local R, C = lpeg.R, lpeg.C
local Cmt, Carg = lpeg.Cmt, lpeg.Carg
local iter_alpha_n_digit
do
local alpha = R "az"
local digit = R "09"
local sequence = Cmt (C((alpha * digit)^1) * Carg (1),
function (_, _, match, fun)
fun (match)
return true
end)
local pattern = (sequence + 1)^1
iter_alpha_n_digit = function (str, fun)
if not str or type (str) ~= "string" then return end
if not fun or type (fun) ~= "function" then return end
return lpegmatch (pattern, str, nil, fun)
end
end
text = "aaab1aaac-aac1d2b5hhpt456d5h9h8"
iter_alpha_n_digit (text, print) -- iterate matches with the print() function
这里,我们将内置的print()函数应用于匹配,但其实是可以替换为任何其他函数的。
2014-01-19 12:46:30
用户107090
尝试这个行业技巧:
text = "aaab1aaac-aac1d2b5hhpt456d5h9h8"
aux = text:gsub("%l%d","\1\1")
for b,e in aux:gmatch("()\1+()") do
print(text:sub(b,e-1))
end
2014-01-19 14:13:52
用户869951
以下是另一种方法,使用一个简单的循环在这种情况下可以工作:
function findzigs(text)
local items = {}
local zigzag = nil
local prevI1=-2
local i1,i2 = text:find("%a%d")
while i1~=nil do
local pair = text:sub(i1,i2)
if i1-2 == prevI1 then
zigzag = zigzag .. pair
else
if zigzag then table.insert(items, zigzag) end
zigzag = pair
end
prevI1 = i1
i1,i2 = text:find("%a%d", i2+1)
end
if zigzag then table.insert(items, zigzag) end
return items
end
可能可以简化以删除重复的“if zigzag”和“text:find”,但您已经了解了这个想法。它恰好提供您需要的结果。
2014-01-20 03:19:00
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
我不知道 Lua,不过这个正则表达式怎么样:
"((?:[a-z][1-9])+)"