如何用MS Word中的一些计算替换括号中的数字 [英] How to replace Numbers in Parentheses with some calculations in MS Word
问题描述
我在MS字词中插入新的引用时,要替换一些序列号,例如MS字中的[30] [31] [32] ...到[31] [32] [33] ...文章的中间.我没有在GUI中找到解决方法,所以我尝试使用VBA进行替换.我在堆栈溢出中发现了类似的问题:
但是,这种方式有点不方便,因为它必须在其他位置生成一些替换数组.我可以用正则表达式和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].有人可以帮忙吗?谢谢!
不可能将回调函数传递给RegExp.Replace
,因此您只有一个选择:使用RegExp.execute
并在循环中处理匹配项. /p>
这是您的案例的示例代码(我采用了一条捷径,因为您只能在已知的定界符[
和]
中修改该值.)
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))
-捕获后追加内容
I have a problem to replace some serial number such as [30] [31] [32]... to [31] [32] [33]... in MS word when I insert a new references in the middle of article. I have not found a solution way in GUI so I try to use VBA to do that replacement. I find a similar problem in stack overflow:
MS Word Macro to increment all numbers in word document
However, this way is a bit inconvenient because it have to generate some replacement array in other place. Can I make that replacement with regex and some function in MS Word VBA like code below?
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
But I don't know how to do some calculations with $1 in regExp? I have try use "[$1+1]" but it return [31+1] [32+1] [33+1]. Can anyone help? Thanks!
It is impossible to pass a callback function to the RegExp.Replace
, so you have the only option: use RegExp.execute
and process matches in a loop.
Here is an example code for your case (I took a shortcut since you only have the value to modify inside known delimiters, [
and ]
.)
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
Here,
Selection.Text = Left(Selection.Text, m.FirstIndex+1)
- Get what is before& Replace(m.Value, m.Value, CStr(CInt(m.Submatches(0)) + 10))
- Add10
to the captured number& Mid(Selection.Text, m.FirstIndex + Len(m.Value))
- Append what is after the capture
这篇关于如何用MS Word中的一些计算替换括号中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!