Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2990|回复: 12

[已解决]学习正则法,这代码应该怎么写。。。

[复制链接]
发表于 2011-9-1 11:40 | 显示全部楼层 |阅读模式
'应该怎么改这个正则法?以下是兰版的正则法(数字相加)
Sub mxg()
    MsgBox ns("正度0.5*0.8") '剔除文字,返回结果公式值0.4   (0.5*0.8)
End Sub

       Function ns(rg)
         Dim reg As New RegExp
         Dim sr, ma, s, m, x
         With reg
           .Global = True
           .Pattern = "\d*\.?\d*"
         Set ma = .Execute(rg)
           For Each m In ma
              s = s + Val(m)
           Next m
         End With
        ns = s
       ' Stop
       End Function
最佳答案
2011-9-1 14:58
回复 兰色幻想 的帖子
  1. Sub mxg()
  2.   MsgBox ns("正度0.5*0.8+(3/2)-1")
  3. End Sub
  4. Function ns(rg As Variant) As String
  5.     With CreateObject("vbscript.regexp")
  6.         .Global = True
  7.         .MultiLine = True
  8.         .Pattern = "[\u4e00-\u9fa5]+"
  9.         If .test(rg) Then
  10.             ns = Format(Application.Evaluate(.Replace(rg, "")), "0.00")
  11.         End If
  12.     End With
  13. End Function
复制代码

{:111:}因为PATTERN多写了一个^
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2011-9-1 12:09 | 显示全部楼层
Sub mxg()
    MsgBox ns("正度0.5*0.8*3") '剔除文字,返回结果公式值0.4   (0.5*0.8)
   
End Sub
       Function ns(rg)
         Dim reg As New RegExp
         Dim sr, ma, s, m, x
         With reg
           .Global = True
           .Pattern = "\d*\.?\d*"
         s = 1
         Set ma = .Execute(rg)
           For Each m In ma
              s = s * IIf(m = "", 1, Val(m))
              Debug.Print s
           Next m
         End With
        ns = s
       End Function
回复

使用道具 举报

 楼主| 发表于 2011-9-1 12:22 | 显示全部楼层
谢谢兰 版。。。

但如果有 其他运算符 就不行了!
有没有通用的?
  MsgBox ns("正度0.5*0.8+(3/2)-1") '剔除文字,返回结果公式值
如:
回复

使用道具 举报

 楼主| 发表于 2011-9-1 12:25 | 显示全部楼层
应该是要改这一句代码吧 ?
.Pattern = "\d*\.?\d*"
回复

使用道具 举报

发表于 2011-9-1 12:26 | 显示全部楼层
.pattern=[^\u4e00-\u9fa5]

if .test(s) then
   msgbox application.evaluate(.replace(s,""))
endif
试试
回复

使用道具 举报

发表于 2011-9-1 12:31 | 显示全部楼层
本帖最后由 兰色幻想 于 2011-9-1 12:31 编辑

Sub mxg()
  MsgBox ns("正度0.5*0.8+(3/2)-1")     
End Sub
       Function ns(rg)
         Dim reg As New RegExp
         Dim ma, s, m, x
         With reg
           .Global = True
           .Pattern = "^\W+"
           ns = Evaluate(.Replace(rg, ""))
        End With
       End Function
回复

使用道具 举报

发表于 2011-9-1 12:34 | 显示全部楼层
花花,你这个正则表达式能运行吗,我怎么不能用呢?
回复

使用道具 举报

 楼主| 发表于 2011-9-1 12:53 | 显示全部楼层
MsgBox ns("0.5正度*0.8+(3/2)-1")     
这样为什么就出错了
本地窗口 看到 NS = ERROR 2015
回复

使用道具 举报

 楼主| 发表于 2011-9-1 13:07 | 显示全部楼层
^ 匹配一个输入或一行的开头,"^\W+"  匹配开头是(非一个字母或数字或下划线,也就是 A~Z,a~z,0~9) 剔除开头是中文的!
回复

使用道具 举报

发表于 2011-9-1 13:35 | 显示全部楼层
如果汉字不在最前面,可以这样
Sub mxg()
  MsgBox ns("正度0.5*0.8我+(3/2)-1")
End Sub
       Function ns(rg)
         Dim reg As New RegExp
         Dim ma, s, m, x, sr
         With reg
           .Global = True
           .Pattern = "[^\.0-9\+\-\*\/\^]"
           sr = .Replace(rg, "")
           ns = Evaluate(sr)
        End With
       End Function
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-15 23:05 , Processed in 0.328110 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表