将字符串拆分为两个变量? LUA

Splitting a String into two variables? LUA

所以在我写的 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

试试这个:

a,b=s:match("(.-)(%d+)$")

这会将字符串末尾的数字捕获到 b 中,并将前面的文本捕获到 a.