有没有办法迭代逗号分隔的字符串,然后用匹配做一些事情?到目前为止,我有:
for a in string.gmatch("this, is, a commaseparated, string", "(.-)[,]") do
print (a)
end
问题是找不到表中的最后一个条目。在C中,可以匹配NULL
以检查您是否在字符串的末尾。 Lua有类似的东西吗?
答案 0 :(得分:4)
试试这个:
for a in string.gmatch("this, is, a commaseparated, string", "([^,]+),?") do
print (a)
end
正则表达式模式([^,]+),?
捕获一个或多个非逗号字符,这些字符可选地后跟逗号。