lua-users home
lua-l archive

Re: GSUB count problem

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Shmuel Zeigerman wrote:
Try this:
 _, count = bytes:gsub("208円169円175円", "")
or this:
 _, count = bytes:gsub(string.char(0xD3,0xA9,0xAF), "")
These methods are not safe, as the characters can have special meaning in the pattern, e.g. "+", "[", etc.
Here's a function that is hopefully safe (not tested):
function count(subj, ...)
 local cnt, start = 0, 1
 local patt = string.char(...)
 while true do
 local s, e = subj:find(patt, start, true)
 if not s then break end
 cnt, start = cnt+1, e+1
 end
 return cnt
end
usage example:
 local cnt = count(subj, 0xD3, 0xA9, 0xAF)
--
Shmuel

AltStyle によって変換されたページ (->オリジナル) /