ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
123
返回列表 发新帖
楼主: jiangweifujian

[求助] 正则表达式把文本中的数学算式提取出来

[复制链接]

TA的精华主题

TA的得分主题

发表于 2021-3-2 11:49 | 显示全部楼层
谢谢楼主分享

TA的精华主题

TA的得分主题

发表于 2021-3-2 13:52 来自手机 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
即使针对你的目前的示例写出成功的代码,后期维护会让你怀疑人生。

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-3-4 15:07 | 显示全部楼层
准提部林 发表于 2021-3-1 13:02
正則雖好用, 但文本也要有規則,
只要有規則, 不一定要用正則,
限計算式固定在最後:

谢谢,受你的启发,一样用Evaluate来盘算是不是算式就好了.
其实我的最终意图是像下面一样,放在加载宏里面,按快捷键后显示当前选中单元格的英文翻译和算式计算.
最终就类似这样写,应该能抓个大概了

Sub fy()
MsgBox "英文翻译: " & Chr(13) & 有道翻译(Selection) & Chr(13) & Chr(13) & "包含算式结果: " & 计算(Selection)
End Sub

Function 计算(txt) As String '计算单元格内算术公式
        If txt.Count > 1 Then
        For Each T In txt
        stxt = stxt & "=" & T
        Next
        End If
        
    txt = stxt
    txt = Application.Substitute(txt, "[", "(")
    txt = Application.Substitute(txt, "]", ")")
    txt = Application.Substitute(txt, "{", "(")
    txt = Application.Substitute(txt, "}", ")")
    txt = Application.Substitute(txt, "X", "*")
    txt = Application.Substitute(txt, "×", "*")
    txt = Application.Substitute(txt, "÷", "/")
    txt = Application.Substitute(txt, ",", "=")
    txt = Application.Substitute(txt, ",", "=")
    txt = Application.Substitute(txt, "。", "=")
    txt = Application.Substitute(txt, ":", "=")
   
On Error Resume Next
a = 正则提取(txt, "[^0-9,+,*,/,=,(,),-]")
If Not a Like "*=*" Then Exit Function
arr = Split(a, "=")
For Each aa In arr
'MsgBox aa
    If aa Like "*#[+,*,/,-]#*" Then
   
    Err.Clear
    Eva = Evaluate(aa) * 1
    If Err.Number > 0 Then Eva = "错误或不是算式"
    If Eva <> aa * 1 Then 计算 = 计算 & Chr(10) & aa & " = " & Eva
   
    End If
Next aa


End Function
Function 正则提取(原内容, 正则表达式) '原内容,正则表达式
   If Len(原内容) < 1 Then Exit Function
   Dim reg As Object: Set reg = CreateObject("vbscript.regexp")
   With reg
      .Global = True
      .Pattern = 正则表达式
      正则提取 = .Replace(原内容, "")
   End With
   Set reg = Nothing
End Function


Function 有道翻译(txt)
    Dim xml
    Dim url$, EngSentence$, s1 As String, str%, sed%, ss$
    aa = Timer

        If txt.Count > 1 Then
        For Each T In txt
        stxt = stxt & " " & T
        Next
        End If

    Set xml = CreateObject("MSXML2.ServerXMLHTTP.6.0")
        url = "http://fanyi.youdao.com/translate?&i=" & stxt & "&doctype=xml&version"
        With xml
            .Open "GET", url, False
              DoEvents
             .send
             DoEvents
            Sleep 300
           ss = .responseText
        End With

str = InStrRev(ss, "<![CDATA[")
sed = InStrRev(ss, "]]>")
有道翻译 = Mid(ss, str + 9, sed - (str + 9))

End Function
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-3-29 21:32 , Processed in 0.037653 second(s), 7 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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