Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LUA count repeating characters in string

I have a string "A001BBD0" and i want to know this info:

  • 0 repeats 3 times
  • B repeats 2 times

and that's it.

I found this pattern on web: "([a-zA-Z]).*(\1)" but it always returns nil for some reason

I guess i should split this string and check each symbol in several loops. I don't think this is a good idea (low performance)

i also found this topic but it doesn't give me any information

like image 935
Ксения Славная Avatar asked Dec 13 '22 14:12

Ксения Славная


1 Answers

gsub returns the number of substitutions. So, try this code:

function repeats(s,c)
    local _,n = s:gsub(c,"")
    return n
end

print(repeats("A001BBD0","0"))
print(repeats("A001BBD0","B"))
like image 164
lhf Avatar answered Dec 23 '22 22:12

lhf