我有一个字符串“A001BBD0”,我想知道这个信息:
就是这样。
我在网上找到了这个模式:“([a-zA-Z]).*(\1)”但由于某种原因它总是返回 nil
我想我应该拆分这个字符串并在几个循环中检查每个符号。我认为这不是一个好主意(低性能)
我还找到了 this 主题,但它没有给我任何信息
最佳答案
为每个字母数字字符创建记录将提供更通用的解决方案
local records = {} -- {['char'] = #number of occurances}
s = "A001BBD0"
for c in string.gmatch(s, "%w") do
if records[c] then
records[c] = records[c] + 1
else
records[c] = 1
end
end
for k,v in pairs(records) do
if(v > 1) then -- print repeated chars
print(k,v)
end
end
-- Output:
-- 0 3
-- B 2
关于string - LUA 计数字符串中的重复字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53210428/