1 function CheckChinese(s) 2 local ret = {}; 3 local f = '[%z\1-\127\194-\244][\128-\191]*'; 4 local line, lastLine, isBreak = '', false, false; 5 for v in s:gfind(f) do 6 table.insert(ret, {c=v,isChinese=(#v~=1)}); 7 end 8 return ret; 9 end10 for k, v in ipairs(CheckChinese('a中文b+')) do 11 print(k,v.c,#v.c,v.isChinese);12 end13 -------------------14 1 a 1 false15 2 中 3 true16 3 文 3 true17 4 b 1 false18 5 + 1 false
贴一段匹配中英文混合输入的代码
1 function dlg:onBtnOKClick() 2 local input = self:getInputNewContent() 3 if input == "" then 4 flytip("输入不能为空") 5 return 6 end 7 if self.pattern then 8 if self.isContainChinese then 9 local charCount = 010 for i, v in ipairs(self:CheckChinese(input)) do 11 if v.isChinese then12 charCount = charCount + 213 else14 charCount = charCount + 115 if string.find(v.c, self.pattern) == nil then16 flytip("抱歉,您输入的内容含有非法字符或屏蔽词,请重新输入。")17 return18 end19 end20 end21 if self.characterLimit then22 if charCount > self.characterLimit then23 flytip("抱歉,您输入的字符长度有误,请重新输入。")24 return25 end26 end27 else 28 if string.find(input, self.pattern) == nil then29 flytip("抱歉,您输入的内容含有非法字符或屏蔽词,请重新输入。")30 return31 end32 end33 end34 35 self:Hide()36 if self._onOK then37 self._onOK()38 end39 end