按键精灵把字符串处理为数值(按键精灵插件源码)

大家好,我是公众号3分钟学堂的郭立员~

去除字符串中的标点符号不算个常用的功能,偶尔会用到,比如做采集脚本时,如果采集用户名称中有表情符号,则无法显示会以问号代替。

通常我想到的解决办法是“过滤”,一段文字中有效的字符就是中文、英文和数字,那么我只需要把这些类型的字符串提取出来行了。

关于在大量字符中提取指定字符,最常用的方式就是正则匹配,在按键中使用的是lua正则匹配,关于数字、字母在lua中都用明确的正则表达式,但是中文没有,一般会用[\128-\254] 这个表达式:

Import "shanhai.lua" Dim str="你好囧犇hello 水电费world3aasdas50849638" Dim 汉字=shanhai.RegexFind(str,"[\128-\254] ") TracePrint 汉字(0)

这个意思是提取ascII码 128-254,如果字符串中含有一些特殊符号,比如表情符号,就会当成中文处理,这样就不准确了。

去掉特殊符号,最开始想到的办法是字符串替换,就是用空值替换掉特殊符号,后来发现由于特殊符号在按键里面都是以问号显示,没法替换。这就不得不想其他办法。

按键精灵既然不行,那就试试lua,于是找了一个lua的处理代码封装成函数,来处理掉这些特殊符号。

代码如下:

function QMPlugin.filter_spec_chars(s) 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) or (c >= 65 and c <= 90) or (c >= 97 and c <= 122) then table.insert(ss, string.char(c)) 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 table.insert(ss, string.char(c, c1, c2)) 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

使用方法是把代码放入文本里面,另存为lua后缀的文件,然后放到按键的插件目录里面。

按键精灵把字符串处理为数值(按键精灵插件源码)(1)

traceprint xm.filter_spec_chars(str)

这个插件功能很多单一,就是去掉各种符号,保留中文、字母和数字。

=正文完=

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页