Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 拆分字符串并在Lua中包含分隔符_String_Split_Lua - Fatal编程技术网

String 拆分字符串并在Lua中包含分隔符

String 拆分字符串并在Lua中包含分隔符,string,split,lua,String,Split,Lua,我在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-

我在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
对于i,单词成对(拆分(文本“”)do
打印(word)
结束
函数拆分(字符串,分隔符)--从https://helloacm.com/split-a-string-in-lua/
结果={};
对于(string..delimiter)中的匹配:gmatch((.-)”.delimiter)do
表.插入(结果、匹配);
结束
返回结果;
结束
此代码将替换格式为“”的部件

例如:

Input: "Hello<a>World</a>!"

Expected Output: {"Hello", "<a>", "World", "</a>", "!"}

Real Output: {"Hello", "World", "!"}
输入:“HelloWorld!”
预期输出:{“您好”、“世界”、“世界”、“世界”、“、!”}
实际输出:{“你好”、“世界”、“!”}

我假设这与HTML标记或类似标记有关

我能想到的一个快速脏化的可能性应该涵盖您的特定用例,就是:

s = 'Hello<a>World</a>!'

function split(s)
  local ans = {}
  for a,b in (s..'<>'):gmatch '(.-)(%b<>)' do
    ans[#ans+1] = a
    ans[#ans+1] = b
  end
  ans[#ans] = nil
  return ans
end

for _,v in ipairs(split(s)) do
  print(v)
end
s='HelloWorld!'
功能拆分
本地ans={}
对于a,b在(s…“):gmatch'(.-)(%b)“do”
ans[#ans+1]=a
ans[#ans+1]=b
结束
ans[#ans]=零
返回ans
结束
对于IPAIR中的v(拆分)do
印刷品(五)
结束

我假设这与HTML标记或类似标记有关

我能想到的一个快速脏化的可能性应该涵盖您的特定用例,就是:

s = 'Hello<a>World</a>!'

function split(s)
  local ans = {}
  for a,b in (s..'<>'):gmatch '(.-)(%b<>)' do
    ans[#ans+1] = a
    ans[#ans+1] = b
  end
  ans[#ans] = nil
  return ans
end

for _,v in ipairs(split(s)) do
  print(v)
end
s='HelloWorld!'
功能拆分
本地ans={}
对于a,b在(s…“):gmatch'(.-)(%b)“do”
ans[#ans+1]=a
ans[#ans+1]=b
结束
ans[#ans]=零
返回ans
结束
对于IPAIR中的v(拆分)do
印刷品(五)
结束
s=“HelloWorld!”
对于s:gsub('%b','\0%0\0')中的a:gmatch'%Z+'do
印刷品(a)
结束
s=“HelloWorld!”
对于s:gsub('%b','\0%0\0')中的a:gmatch'%Z+'do
印刷品(a)
结束

可以尝试
string.gmatch
来代替。string.gmatch排除分隔符
string。gmatch
包括您告诉它包含的任何内容:
对于v in(“a,b,c,d”):gmatch((%w+,?))进行打印(v)结束
。显示示例输入和预期输出。使用示例和我正在使用的代码更新问题。gmatch可以尝试使用字符串。string.gmatch排除分隔符
字符串。gmatch
包括您告诉它包括的任何内容:
用于v in(“a,b,c,d”):gmatch(((%w+,?))。请打印(v)结束
。显示示例输入和预期输出。使用示例和我正在使用的代码更新问题