Word VBA 中的模式匹配查找

Word VBA 中的模式匹配查找

我正在尝试帮助某人使用 VBA 自动执行 Microsoft Word 中的一些非常简单的文本编辑。

一个理想的更改是删除嵌入在数字中的逗号,同时保留其他逗号。例如:

I like apples, pears, and plums.  See 1,234,567

会成为:

I like apples, pears, and plums.  See 1234567

代码会查找一个数字,后面跟着一个逗号,后面跟着一个数字。要删除两个数字之间的逗号,当前代码会循环遍历所有 100 种可能的情况,从0,09,9Find/Replace并对每个案例执行。因此0,0变成00。以下是当前代码:

Sub FindAndReplace()

Dim rng As Range, sFind As String, sRep As String
Dim i As Long, j As Long

For i = 0 To 9
    For j = 0 To 9
        sFind = i & "," & j
        sRep = i & j
        For Each rng In ActiveDocument.StoryRanges
            With rng.Find
                .Text = sFind
                .Replacement.Text = sRep
                .Wrap = wdFindContinue
                .Execute Replace:=wdReplaceAll
            End With
        Next rng
        Next j
    Next i
lbl_Exit:
Exit Sub

我确信所有这些循环i都是j 完全没有必要的,这可以用一个简单的通配符模式来完成Find,但我对 Word 对象模型了解不够,无法实现这一点。

更新:

RexEx 解决方案非常有效。我还想出了一个通配符解决方案:

Sub CommaKiller()
    Dim rngStory As Range

        For Each rngStory In ActiveDocument.StoryRanges
            With rngStory.Find
                .MatchWildcards = True
                .Text = "([0-9]),([0-9])"
                .Replacement.Text = "\1\2"
                .Wrap = wdFindContinue
                .Execute Replace:=wdReplaceAll
            End With
        Next rngStory
End Sub

答案1

您可以尝试使用 Microsoft VBScript Regular Expressions 5.5 库:

在此处输入图片描述

然后,您可以构建一个适当的正则表达式模式并让其返回文本中的匹配项,然后从这些匹配项中删除逗号并将清理后的版本返回到文本:

Option Explicit

Public Sub testremovecomma()

Dim txt As String
Dim results As Object
Dim result As Object
Dim r As New RegExp

txt = "I like apples, pears, 2000 6546 and plums.  See 1,234,567"

'a space, followed by:
'between one and three digits followed by a comma (at least one time)
'followed by a group of three digits
r.Pattern = "\s(\d{1,3},+){1,}(\d{3})"

'find all matches, not just the first one
r.Global = True

'get all the matches
Set results = r.Execute(txt)

'for each match, remove the commas and put it back in the text
For Each result In results
    txt = Replace(txt, result, Replace(result, ",", ""))
Next result

Debug.Print txt

End Sub

我输入了一些随机数进行测试,以确保它只能找到包含逗号的数字。

这当然是特定于文化的,在印度尝试使用这个精确的正则表达式可能不适用于所有数字。不过,我确实认为这种方法很好,您可以根据需要调整正则表达式。

我使用并推荐此网站来测试正则表达式:

https://regex101.com/

在此处输入图片描述

相关内容