Lua的gsub大小写和frontier模式

使用 Lua 5.1 和 gsub 函数,如何实现大小写敏感/不敏感和边界模式匹配选项以计算子字符串的数量?

CountSubString = function(s,CtrlID)              --< s = string to count
  local t = Scintilla.GetText(CtrlID)
  if FindReplace.FindCase == 0 then               --< 不区分大小写
   if FindReplace.FindWhole == 0 then             --< 不匹配整个单词
    local _,count = string.gsub(t,s," ")
    return "Found "..count .." occourances of "..s
   else                                           --< 匹配整个单词
    local _,count = string.gsub(t,"(%W)"..s.."(%W)","%1%2")
    return "Found "..count .." occourances of "..s
   end
  else                                            --< 区分大小写
   if FindReplace.FindWhole == 0 then             --< 不匹配整个单词
    local _,count = string.gsub(t,s," ")
    return "Found "..count .." occourances of "..s
   else                                           --< 匹配整个单词
    local _,count = string.gsub(t,"(%W)"..s.."(%W)","%1%2")
    return "Found "..count .." occourances of "..s
   end
  end
 end;

我在这里有一个较旧的帖子,其中包含 frontier 模式,但它也包含大小写/非大小写的选项。

点赞