如何用MS Word中的某些计算替换括号中的数字

时间:2017-05-17 04:33:09

标签: regex vba ms-word preg-replace word-vba

我有一个问题,在我插入一个新的引用时,在MS字中替换某些序列号,如[30] [31] [32] ...到[31] [32] [33] ....文章中间。我还没有在GUI中找到解决方法,所以我尝试使用VBA进行替换。我在堆栈溢出中发现了类似的问题:

MS Word Macro to increment all numbers in word document

然而,这种方式有点不方便,因为它必须在其他地方生成一些替换阵列。我可以使用正则表达式和MS Word VBA中的某些功能替换下面的代码吗?

Sub replaceWithregExp()

Dim regExp As Object
Dim regx, S$, Strnew$
Set regExp = CreateObject("vbscript.regexp")
With regExp
    .Pattern = "\[([0-9]{2})\]"
    .Global = True
End With
'How to do some calculations with $1?
Selection.Text = regExp.Replace(Selection.Text, "[$1]")
End Sub

但是我不知道如何用regExp中的$ 1进行一些计算?我尝试使用“[$ 1 + 1]”,但它返回[31 + 1] [32 + 1] [33 + 1]。有人可以帮忙吗?谢谢!

3 个答案:

答案 0 :(得分:2)

无法将回调函数传递给RegExp.Replace,因此您有唯一的选择:使用RegExp.execute并在循环中处理匹配。

以下是您的案例的示例代码(我使用了一个快捷方式,因为您只有在已知分隔符[]内修改的值。)

Sub replaceWithregExp()

Dim regExp As Object
Dim regx, S$, Strnew$
Set regExp = CreateObject("vbscript.regexp")
With regExp
    .Pattern = "\[([0-9]{2})]"
    .Global = True
End With
'How to do some calculations with $1?
' Removing regExp.Replace(Selection.Text, "[$1]")
For Each m In regExp.Execute(Selection.Text)
    Selection.Text = Left(Selection.Text, m.FirstIndex+1) _
        & Replace(m.Value, m.Value, CStr(CInt(m.Submatches(0)) + 10)) _
        & Mid(Selection.Text, m.FirstIndex + Len(m.Value))
Next m

End Sub

在这里,

  • Selection.Text = Left(Selection.Text, m.FirstIndex+1) - 获取之前的内容
  • & Replace(m.Value, m.Value, CStr(CInt(m.Submatches(0)) + 10)) - 将10添加到捕获的号码
  • & Mid(Selection.Text, m.FirstIndex + Len(m.Value)) - 追加捕获后的内容

答案 1 :(得分:1)

这是一个简单的VBA宏,可用于实现此目的:

Sub IncrementNumbers()

    Dim regExp As Object
    Dim i As Integer
    Dim fullMatch As String
    Dim subMatch As Integer
    Dim replacement As String
    Const TMP_PREFIX As String = "$$$"

    Set regExp = CreateObject("vbscript.regexp")
    With regExp
        .Pattern = "\[([0-9]{2})\]"
        .Global = True
        .MultiLine = True
    End With

    'Ensure selected text match our regex
    If regExp.test(Selection.Text) Then
        'Find all matches
        Set matches = regExp.Execute(Selection.Text)
        ' Start from last match
        For i = 0 To (matches.Count - 1)
            fullMatch = matches(i).Value
            subMatch = CInt(matches(i).SubMatches(0))
            'Increment by 1
            subMatch = subMatch + 1
            'Create replacement. Add a temporary prefix so we ensure [30] replaced with [31]
            'will not be replaced with [32] when [31] will be replaced
            replacement = "[" & TMP_PREFIX & subMatch & "]"
            'Replace full match with [subMatch]
            Selection.Text = Replace(Selection.Text, fullMatch, replacement)
        Next
    End If

    'Now replacements are complete, we have to remove replacement prefix
    Selection.Text = Replace(Selection.Text, TMP_PREFIX, "")

End Sub

答案 2 :(得分:1)

应该这样做:

Sub IncrementWithRegex()
    Dim Para As Paragraph
    Set Para = ThisDocument.Paragraphs.First
    Dim ParaNext As Paragraph
    Dim oRange As Range
    Set oRange = Para.Range
    Dim regEx As New RegExp
    Dim regMatch As Variant
    Dim ACrO As String

    With regEx
        .Global = True
        .MultiLine = False
        .IgnoreCase = False
        .Pattern = "[\[]([0-9]{2})[\]]"
    End With

    Do While Not Para Is Nothing
        Set ParaNext = Para.Next
        Set oRange = Para.Range
        'Debug.Print oRange.Text
        If regEx.test(oRange.Text) Then
            For Each regMatch In regEx.Execute(oRange.Text)
                oRange.Text = _
                    Left(oRange.Text, _
                         InStr(1, oRange.Text, CStr(regMatch))) & _
                    CDbl(regMatch) + 1 & _
                    Right(oRange.Text, _
                          Len(CStr(regMatch)) + InStr(1, oRange.Text, CStr(regMatch)))
            Next regMatch
        Else
        End If
        Set Para = ParaNext
    Loop
End Sub

要使用此功能,请记住添加引用:

Description: Microsoft VBScript Regular Expressions 5.5
FullPath: C:\windows\SysWOW64\vbscript.dll\3
Major.Minor: 5.5
Name:     VBScript_RegExp_55
GUID: {3F4DACA7-160D-11D2-A8E9-00104B365C9F}