我正在学习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
但是它不能打印正确的结果
试试这个技巧:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句