以点分割字符串lua

我正在尝试通过单点拆分字符串,同时保留双(甚至更多)点。

我的方法是这样的,只适用于双点:

local s = "some string.. with several dots, added....more dots.another line inserted."; for line in s:gsub('%.%.','#&'):gmatch('[^%.]+') do print(line:gsub('#&','..')); end

另一种方法是这样的:

print(s:match('([^%.]+[%.]*[^%.]+)'))

它会在下一个点序列之后停止,所以不适合。

如何在模式匹配中实现这一目标?

点赞
用户3735873
用户3735873

另一种方法:

local s = 'some string.. with several dots, added....more dots.another line inserted.'

function split_on_single_dot(s)
  local ans, old_pos = {}, 1
  for pos,dots in (s..(s:sub(-1) == '.' and '' or '.')):gmatch '()(%.+)' do
    if #dots == 1 then
      ans[#ans+1] = s:sub(old_pos,pos-1)
      old_pos = pos+1
    end
  end
  return ipairs(ans)
end

-- 测试
for i,v in split_on_single_dot(s) do print(i,v) end
2019-06-23 18:36:47
用户1847592
用户1847592
```lua
local s = 'some string.. with several dots, added....more dots.another line inserted.'
for line in s:gsub("%f[.]%.%f[^.]", "\0"):gmatch"%Z+" do
   print(line)
end

```lua
local s = 'some string.. with several dots, added....more dots.another line inserted.'
for line in s:gsub("%f[.]%.%f[^.]", "\0"):gmatch"%Z+" do
   print(line)
end
2019-06-23 19:55:17