在数字上拆分字符串

时间:2020-12-21 03:07:18

标签: lua

如果我有一个像 "123abc456def" 这样的字符串,我怎么能把它放到像 {"123abc","456def"} 这样的表中(分割数字,但保留它)。

我找到了 string.match(),但它删除了它所分割的字母,至少使用模式(这是正确的词吗?)我一直在使用,但我不知道如何制作它们我自己。

1 个答案:

答案 0 :(得分:1)

一种选择是在模式 gmatch 上使用 %d+%a+,它匹配一系列数字后跟字母:

t = {}
s = "123abc456def"
count = 0
for m in string.gmatch(s, "%d+%a+") do
    t[count] = m
    count = count + 1
    print(m)
end

打印:

123abc
456def
相关问题