Regex Word 2013中的正则表达式模式
我有一个word文档,其中包含6个数字系列(纯文本,非编号样式),如下所示:Regex Word 2013中的正则表达式模式,regex,vba,ms-word,word-2013,Regex,Vba,Ms Word,Word 2013,我有一个word文档,其中包含6个数字系列(纯文本,非编号样式),如下所示: 1) blah blah blah 2) again blah blah blah . . . 20) something 这种模式已经重复了六次。如何使用正则表达式并将括号前的所有数字序列化,使其以1开头,以120结尾?您可以使用VBA-将其添加到ThisDocument模块: Public Sub FixNumbers() Dim p As Paragraph Dim i As Long
1) blah blah blah
2) again blah blah blah
.
.
.
20) something
这种模式已经重复了六次。如何使用正则表达式并将括号前的所有数字序列化,使其以
1
开头,以120
结尾?您可以使用VBA-将其添加到ThisDocument模块:
Public Sub FixNumbers()
Dim p As Paragraph
Dim i As Long
Dim realCount As Long
realCount = 1
Set p = Application.ActiveDocument.Paragraphs.First
'Iterate through paragraphs with Paragraph.Next - using For Each doesn't work and I wouldn't trust indexing since we're making changes
Do While Not p Is Nothing
digitCount = 0
For i = 1 To Len(p.Range.Text)
'Keep track of how many characters are in the number
If IsNumeric(Mid(p.Range.Text, i, 1)) Then
digitCount = digitCount + 1
Else
'We check the first non-number character we find to see if it is the list delimiter ")" and we make sure that there were some digits before it
If Mid(p.Range.Text, i, 1) = ")" And digitCount > 0 Then
'If so, we get rid of the original number and put the correct one
p.Range.Text = realCount & Right(p.Range.Text, Len(p.Range.Text) - digitCount) 'It's important to note that a side effect of assigning the text is that p is set to p.Next
'realCount holds the current "real" line number - everytime we assign a line, we increment it
realCount = realCount + 1
Exit For
Else
'If not, we skip the line assuming it's not part of the list numbering
Set p = p.Next
Exit For
End If
End If
Next
Loop
End Sub
您可以通过单击代码内部的任意位置并单击VBA IDE中的“播放”按钮来运行它。我认为您可以更改编号设置,使其继续,而不是每次都从头开始。正则表达式没有数字加法的概念。@npinti我提到过它们不是
编号的样式。它们是纯文本。Word不支持正则表达式,只能使用一些有限的模式。宏可能在这里有所帮助。让我们添加一个VBA标记。在前面没有数字的文本行中,是否有应该跳过的文本行?