LOFTER for ipad —— 让兴趣,更有趣

点击下载 关闭
利用正则表达式取汉字、数字、字母的方法
杂物箱 2017-03-10

非常实用的正则表达实例,感谢会员游9大大的分享,关于正则的优势,我想大家都懂的,下面咱们就来说下按键精灵中利用正则表达式获取汉子、数字、字母的方法
'正则取汉字!

str = "山a里b有c322个和d尚一e次打f水 打了2233桶水" Dim Rlt() Dim regEx, Match, Matches, i i = 0 Set regEx = New RegExp regEx.IgnoreCase = True Execute "regEx.Global = True" regEx.pattern = "[u4E00-u9FA5]" Set Matches = regEx.Execute(str) ReDim Rlt(Matches.Count) For Each Match in Matches     Rlt(i) = Match.Value     i = i + 1 Next MsgBox Join(rlt,"") 

'正则取数字!

str = "山a里b有c322个和d尚一e次打f水 打了2233桶水" Set regEx = New RegExp regEx.IgnoreCase = True Execute "regEx.Global = True" regEx.pattern = "d+" Set Matches = regEx.Execute(str) ReDim Rlt(Matches.Count) For Each Match in Matches     Rlt(i) = Match.Value     i = i + 1 Next MsgBox Join(rlt,"")

'正则取字母!

str = "山a里b有c322个和d尚一e次打f水 打了2233桶水" Dim Rlt() Dim regEx, Match, Matches, i i = 0 Set regEx = New RegExp regEx.IgnoreCase = True Execute "regEx.Global = True" regEx.pattern = "[a-zA-Z]" Set Matches = regEx.Execute(str) ReDim Rlt(Matches.Count) For Each Match in Matches Rlt(i) = Match.Value i = i + 1 Next MsgBox Join(rlt,"")

-------------------------------------
正则通用函数版(进阶篇)
语法:返回值=正则(参数1, 参数2)
参数1:字符串,放入想过滤的字符串
参数2:整数,想过滤的模式,0为取汉字,1为取数字,2为取英文字母,3为取英文字母和数字,4为排除数字
另外可使用字符串模式
ex:"12345abcde" 代表只取数字12345跟英文小写abcde
ex:"^12345abcde" 代表排除数字12345跟英文小写abcde
如果想取符号的话,在想要取的符号前面加
ex:",.?:;" 代表取符号 ,.?:;
返回值:字符串,取出之后的结果

应用范例:

汉字=正则("a536小b4冉4cj1521博d11ja客Dg",0) TracePrint 汉字 ' 返回=小冉博客 数字=正则("ranlingqi.com/wordpress500bug.html",1) TracePrint 数字 ' 返回=500   //如果是500bug1.html 则返回 5001 字母=正则("ranlingqi.com/wordpress500bug.html",2) TracePrint 字母 ' 返回=ranlingqicomwordpressbughtml //---模式3,模式4就不举例了,自己尝试吧!~~~ 取字符串=正则("a536小b4冉4cj1521博d11ja客Dg","a4b") TracePrint 取字符串 ' 返回=ab44a 排除字符串=正则("a536小b4冉4cj1521博d11ja客Dg","^a45b36") TracePrint 排除字符串 ' 返回=小冉cj1,21博d11j客Dg Function 正则(字符串, 模式)     //--模式0=取汉字,模式1=取数字,模式2=取英文字母,模式3=取英文字母和数字,模式4=排除数字     //--模式为字符串=只取或排除该字符串内有的字符     Dim mode,TQstring     Select Case 模式     Case 0         mode="[u4E00-u9FA5]"     Case 1         mode="d+"     Case 2         mode = "[a-zA-Z]"     Case 3         mode = "[a-zA-Z0-9]"     Case 4         mode =  "[^0-9]"     Case  Else         mode = "[" & 模式 & "]"     End Select     TQstring = "Dim i" & vbCrLf     TQstring = TQstring & "Dim regEx, Match, Matches" & vbCrLf     TQstring = TQstring & "i = 0" & vbCrLf     TQstring = TQstring & "Set regEx = New RegExp " & vbCrLf     TQstring = TQstring & "regEx.IgnoreCase = True " & vbCrLf     TQstring = TQstring & "regEx.Global = True" & vbCrLf     TQstring = TQstring & "regEx.pattern ="""& mode &"""" & vbCrLf     TQstring = TQstring & "Set Matches = regEx.Execute("""& 字符串 &""")" & vbCrLf     TQstring = TQstring & "ReDim Rlt(Matches.Count)" & vbCrLf     TQstring = TQstring & "For Each Match in Matches  " & vbCrLf     TQstring = TQstring & "Rlt(i) = Match.Value" & vbCrLf     TQstring = TQstring & " i = i + 1" & vbCrLf     TQstring = TQstring & "Next" & vbCrLf     TQstring = TQstring & "Set regEx = Nothing"     Execute TQstring     正则 = Join(Rlt, "") End Function


推荐文章
评论(0)
联系我们|招贤纳士|移动客户端|风格模板|官方博客|侵权投诉 Reporting Infringements|未成年人有害信息举报 0571-89852053|涉企举报专区
网易公司版权所有 ©1997-2024  浙公网安备 33010802010186号 浙ICP备16011220号-11 增值电信业务经营许可证:浙B2-20160599
网络文化经营许可证: 浙网文[2022]1208-054号 自营经营者信息 工业和信息化部备案管理系统网站 12318全国文化市场举报网站
网信算备330108093980202220015号 网信算备330108093980204230011号
分享到
转载我的主页