您的位置:首页 > 编程语言 > Lua

lua指定规则匹配字符

2014-11-07 16:14 274 查看
--是否是合格字符串
function isLegal(str)
local len = string.len(str)
local base = {}

local basePregStr = "_,.!?;:"
local basePregStrLen = string.len(basePregStr)

for i=1,basePregStrLen do

table.insert(base,string.byte(basePregStr,i,i))
end

--0-9 a-z A-Z
local numCode = string.byte("0",1,1)
for i=1,10 do

table.insert(base,numCode)
numCode = numCode + 1
end

local wordCode_lower = string.byte("a",1,1)
local wordCode_upper = string.byte("A",1,1)

for i=1,26 do

table.insert(base,wordCode_lower)
table.insert(base,wordCode_upper)
wordCode_upper = wordCode_upper + 1
wordCode_lower = wordCode_lower + 1
end

-- for k,v in pairs(base) do

--     print(k,'=>',string.format("%c",v))
-- end

function isInBase(n)
local isIn = false
for _,v in pairs(base) do
if v == n then
isIn = true
break
end
end

return isIn
end

local count = 0
for i=1,len do

if count > 0 then
count = count - 1

else
print("i=>",i)
local strNum = string.byte(str,i,i)

if strNum < 127 then --汉字大于127

if isInBase(strNum) == false then
print("不在base中",i)
return false
end
else
--判断是否 是汉字 而不是特殊字符
local s1 = strNum
local s2 = string.byte(str,i+1,i+1)
local s3 = string.byte(str,i+2,i+2)
local s4 = s1..''..s2..''..s3

local num = tonumber(s4)

-- 汉字4e00 9fa5
-- lua中的范围 228184128   233190165
if num < 228184128 or num > 233190165 then
return false
end
count = 2
end

end
end

return true
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lua正则匹配