本帖最后由 duquancai 于 2016-4-22 18:22 编辑
记得有一题目“在word里有一串字母,比如abcdabcdaaabbaabcaaaaaa.......一连串字母,要求搜素出“abc”这个固定排列,并且把“abc"后面接着的6个字母提取出来。 如上面的例子,就是要abc后面的,把“dabcda" "daaabb" "aaaaaa" ,找出来,并且全部复制提取出来。”,详见:http://club.excelhome.net/forum.php?mod=viewthread&tid=1223907。现在用“word中利用正则表达式查找替换”来解答,在Word文档中有内容:abcdabcdaaabbaabcaaaaaa.......下列事例代码:
Sub word正则查找替换()
Dim myString As Variant
Application.ScreenUpdating = False
Set reg = CreateObject("vbscript.regexp")
myString = ActiveDocument.Content
With reg
.Pattern = "(?=(abc[a-z]{6}))"
.Global = True
.IgnoreCase = False
.MultiLine = True
myString = .Replace(myString, ",$1")
End With
ActiveDocument.Content.Text = myString
With Selection
.EndKey wdStory
.Delete
.HomeKey wdStory
End With
Set myString = Nothing
Set reg = Nothing
Application.ScreenUpdating = True
End Sub
运行上面代码后,在word文档中内容如下:
,abcdabcdaabcd,abcdaaabbabcdaaabba,abcaaaaaaabcaaaaaa....... 本事例只是抛砖引玉。
|