【发布时间】:2016-03-03 18:49:39
【问题描述】:
我在 Google 和 Stack Overflow 上进行了搜索,但没有找到这个问题的答案。查看文档我没有找到如何执行此操作,因为每个允许拆分的函数都排除了分隔符。
编辑
for i, word in pairs(split(text, "<(.-)>")) do
print(word)
end
function split(string, delimiter) -- Got this function from https://helloacm.com/split-a-string-in-lua/
result = {};
for match in (string..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
此代码替换格式为“”的部分
例子:
Input: "Hello<a>World</a>!"
Expected Output: {"Hello", "<a>", "World", "</a>", "!"}
Real Output: {"Hello", "World", "!"}
【问题讨论】:
-
也许可以试试
string.gmatch。 -
string.gmatch 不包括分隔符
-
string.gmatch包含您告诉它包含的任何内容:for v in ("a, b, c, d"):gmatch("(%w+,?)") do print(v) end。 -
显示示例输入和预期输出。
-
用一个例子和我正在使用的代码更新了问题