Find 方法搜索变量中带有特殊字符的文本

发布于 2025-01-15 02:43:44 字数 1214 浏览 4 评论 0原文

我正在尝试创建一个宏来查找所有文本公式并将其替换为 OMath 公式。 我创建了一个文本,其中所有公式都被特殊标签包围(在我的例子中是“公式”一词)。然后我用正则表达式查找所有案例并创建包含找到的公式的变量。然后我给这个变量提供查找方法来创建范围,然后我对其进行修改。 某些公式包含特殊字符(在我的例子中是插入符号 (^),用于创建幂)并且 find 方法不会选择它们。 如何忽略为 find 方法提供的变量中的特殊字符?

我创建的宏:

Dim regexObject As Object
Set regexObject = CreateObject("VBScript.RegExp")
Dim matches As Object
Dim objEq As OMath
Dim objRange As Range
Dim match As Object


regexObject.Pattern = "formula(.*?)formula"
regexObject.MultiLine = True

Selection.WholeStory
While regexObject.test(Selection.Text)
    Set matches = regexObject.Execute(Selection.Text)
    For Each match In matches
    MsgBox match
        With Selection.Find
            .Text = match
            .MatchWildcards = False
            .Execute
            Set objRange = Selection.Range
            objRange.Text = Mid(match, 8, Len(match) - 14)
            Set objRange = Selection.OMaths.Add(objRange)
            Set objEq = objRange.OMaths(1)
            objEq.BuildUp
        End With
        Selection.WholeStory
    Next
Wend

示例文本:

formulaΨ=1,67∙0,72∙0,9∙1=1,09.formula
formulac_c=0,9formula
formulaE_q=Ψ WV_n^2/2,formula
formulac_m, c_e, c_c, c_sformula

I am trying to create a macro that finds all text formulas and replaces it with OMath formulas.
I create a text, where all formulas are surrounded with special tags (in my case its word "formula"). Then I find all cases with regex and create variable that contains found formula. Then I give this variable to find method to create range, which I then modify.
Some formulas contain special characters (in my case its caret (^), which is used to create power) and find method doesn't select them.
How can I ignore special characters in variable that I give to find method?

Macro that I created:

Dim regexObject As Object
Set regexObject = CreateObject("VBScript.RegExp")
Dim matches As Object
Dim objEq As OMath
Dim objRange As Range
Dim match As Object


regexObject.Pattern = "formula(.*?)formula"
regexObject.MultiLine = True

Selection.WholeStory
While regexObject.test(Selection.Text)
    Set matches = regexObject.Execute(Selection.Text)
    For Each match In matches
    MsgBox match
        With Selection.Find
            .Text = match
            .MatchWildcards = False
            .Execute
            Set objRange = Selection.Range
            objRange.Text = Mid(match, 8, Len(match) - 14)
            Set objRange = Selection.OMaths.Add(objRange)
            Set objEq = objRange.OMaths(1)
            objEq.BuildUp
        End With
        Selection.WholeStory
    Next
Wend

Sample Text:

formulaΨ=1,67∙0,72∙0,9∙1=1,09.formula
formulac_c=0,9formula
formulaE_q=Ψ WV_n^2/2,formula
formulac_m, c_e, c_c, c_sformula

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∝单色的世界 2025-01-22 02:43:44

例如:

Sub FindFormulaeCreateOMath()
    Dim objEq As OMath
    Dim findRange As Range
    Dim eqRange As Range
    
    Set findRange = ActiveDocument.Content
    With findRange
        With .Find
            .Text = "formula*formula"
            .MatchWildcards = True
        End With
        Do While .Find.Execute
            .Text = Mid(.Text, 8, Len(.Text) - 14)
            Set eqRange = findRange.OMaths.Add(.Duplicate)
            Set objEq = eqRange.OMaths(1)
            objEq.BuildUp
            .Collapse wdCollapseEnd
        Loop
    End With
End Sub

For example:

Sub FindFormulaeCreateOMath()
    Dim objEq As OMath
    Dim findRange As Range
    Dim eqRange As Range
    
    Set findRange = ActiveDocument.Content
    With findRange
        With .Find
            .Text = "formula*formula"
            .MatchWildcards = True
        End With
        Do While .Find.Execute
            .Text = Mid(.Text, 8, Len(.Text) - 14)
            Set eqRange = findRange.OMaths.Add(.Duplicate)
            Set objEq = eqRange.OMaths(1)
            objEq.BuildUp
            .Collapse wdCollapseEnd
        Loop
    End With
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文