所以在LUA驱动程序中,我正在编写,我一直在接收RS232字符串,例如; ZAA1,ZO64,D1等。我正在寻找一种解决方案,以查找字符串的结尾处,然后从Int开始并将其放入两个不同的变量中? 我目前正在使用带有string.match方法的while循环。有没有更好的办法?下面的当前缩短的代码;
s = "ZO29"
j = 1
while j <= 64 do
if (s == string.format("ZO%d", j)) then
print("Within ZO message")
inputBuffer = ""
sendACK()
break
elseif (s == string.format("ZC%d", j)) then
inputBuffer = ""
sendACK()
break
end
j = j + 1
end
答案 0 :(得分:2)
尝试一下:
a,b=s:match("(.-)(%d+)$")
这会将字符串末尾的数字捕获到b
中,并将前面的文本捕获到a
中。