如何用 MS Word 中的一些计算替换括号中的数字
How to replace Numbers in Parentheses with some calculations in MS Word
当我插入新的参考文献时,我无法在 MS word 中替换一些序列号,例如 [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 ?
Selection.Text = regExp.Replace(Selection.Text, "[]")
End Sub
但我不知道如何在 regExp 中用 $1 进行一些计算?我尝试使用“[$1+1]”,但它 return [31+1] [32+1] [33+1]。谁能帮忙?谢谢!
无法将回调函数传递给 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 ?
' Removing regExp.Replace(Selection.Text, "[]")
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))
- 追加捕获后的内容
这是一个简单的 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
这应该可以解决问题:
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
Major.Minor: 5.5
Name: VBScript_RegExp_55
GUID: {3F4DACA7-160D-11D2-A8E9-00104B365C9F}
当我插入新的参考文献时,我无法在 MS word 中替换一些序列号,例如 [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 ?
Selection.Text = regExp.Replace(Selection.Text, "[]")
End Sub
但我不知道如何在 regExp 中用 $1 进行一些计算?我尝试使用“[$1+1]”,但它 return [31+1] [32+1] [33+1]。谁能帮忙?谢谢!
无法将回调函数传递给 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 ?
' Removing regExp.Replace(Selection.Text, "[]")
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))
- 追加捕获后的内容
这是一个简单的 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
这应该可以解决问题:
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
Major.Minor: 5.5
Name: VBScript_RegExp_55
GUID: {3F4DACA7-160D-11D2-A8E9-00104B365C9F}