Lua实现特殊字符过滤

lmc994464212qq 2018-05-08 01:34:57
function filter_spec_chars(s,charTypes)
local ss = {}
local k = 1
while true do
if k > #s then break end
local c = string.byte(s,k)
if not c then break end
if c<192 then
if (c>=48 and c<=57) then
if charTypes.num then
table.insert(ss, string.char(c))
end
elseif (c>= 65 and c<=90) or (c>=97 and c<=122) then
if charTypes.char then
table.insert(ss, string.char(c))
end
end
k = k + 1
elseif c<224 then
k = k + 2
elseif c<240 then
if c>=228 and c<=233 then
local c1 = string.byte(s,k+1)
local c2 = string.byte(s,k+2)
if c1 and c2 then
local a1,a2,a3,a4 = 128,191,128,191
if c == 228 then a1 = 184
elseif c == 233 then a2,a4 = 190,c1 ~= 190 and 191 or 165
end
if c1>=a1 and c1<=a2 and c2>=a3 and c2<=a4 then
if charTypes.chs then
table.insert(ss, string.char(c,c1,c2))
end
end
end
end
k = k + 3
elseif c<248 then
k = k + 4
elseif c<252 then
k = k + 5
elseif c<254 then
k = k + 6
end
end
return table.concat(ss)
end

local function main()
print(filter_spec_chars("123你好OI+)*&^#@163.com", {num="数字",char="字母",chs="中文"}))
end
main()
...全文
1930 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
miniBirds 2019-04-12
  • 打赏
  • 举报
回复
-- RuneLen returns the number of bytes required to encode the rune. -- It returns -1 if the rune is not a valid value to encode in UTF-8. local surrogateMin = 55296 --0xD800 local surrogateMax =57343 -- 0xDFFF local rune1Max = 127 --1<<7 - 1 local rune2Max = 2047 -- 1<<11 - 1 local rune3Max =65535 -- 1<<16 - 1 local MaxRune = 1114111-- "\U0010FFFF" -- Maximum valid Unicode code point. function RuneLen(r) if r < 0 then return -1 elseif r <= rune1Max then return 1 elseif r <= rune2Max then return 2 elseif surrogateMin <= r and r <= surrogateMax then return -1 elseif r <= rune3Max then return 3 elseif r <= MaxRune then return 4 else return -1 end end local str="a你😊b好3😊c" for p, c in utf8.codes(str) do print("#############################################") print(p .."=".. c .."("..RuneLen(c)..")") end
zerozerg2006 2018-05-31
  • 打赏
  • 举报
回复
引用 1 楼 lovesmiles 的回复:
你这种东西不实用,几个M文件的过滤会卡死
其实也不能这么说,让lua使用扫描的方法去处理几M的文件本身就不合理。一个脚本语言不应该期望和C一样强大。 我猜楼主是想处理几十个字符的字符串吧
勤奋的小游侠 2018-05-30
  • 打赏
  • 举报
回复
你这种东西不实用,几个M文件的过滤会卡死
内容概要:本文围绕水陆两栖无人机的任务规划与执行问题,提出了一种基于Matlab实现的智能路径规划解决方案,重点融合粒子群优化算法(PSO)与遗传算法(GA)进行三维环境下的避障路径规划。研究系统性地构建了从复杂地形建模、任务需求分析到算法设计与仿真实验的完整流程,实现了在多约束、动态障碍物等复杂环境下无人机的高效任务调度。通过对GA与PSO两种智能优化算法在路径长度、收敛速度、路径平滑度及稳定性等方面的对比分析,深入探讨了各自在无人机路径规划中的适用场景与性能差异,验证了所提方法在提升任务执行效率与安全性方面的有效性。该方案不仅适用于水陆交互通用场景,也为多模态无人系统自主导航提供了可扩展的技术框架。; 适合人群:具备一定Matlab编程能力和算法基础,从事无人机路径规划、智能优化算法研究、自动化控制或相关领域科研工作的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于水陆两栖无人机在复杂自然环境(如江河湖海与陆地交错区域)中的自主巡航与任务执行;②比较遗传算法与粒子群算法在三维动态路径规划中的优化性能,指导实际工程中算法选型;③为多约束条件下无人系统的自主决策与实时避障提供算法支持与仿真验证平台。; 阅读建议:建议读者结合提供的Matlab代码进行动手实践,重点关注两种算法的数学建模过程、适应度函数设计、参数调优策略及路径生成逻辑,通过仿真实验直观对比算法性能差异,深入理解智能优化算法在复杂路径规划问题中的应用机制与优化潜力。

726

社区成员

发帖
与我相关
我的任务
社区描述
Cocos2d-x相关内容讨论专区
社区管理员
  • Cocos2d-x
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧