四时宝库

程序员的知识宝库

Word VBA|用正则表达式进行文本的查找与替换

如在word中有类似以下文本:

只想把数字后的逗号替换为空格,其它保持不变,可以在VBA使用正则表达式。

其它类似的查找和替换只要给.Pattern属性赋不同的值即可。至于正则表达式的构造在不同的编程语言中都是大同小异。

附代码:

Sub replacetxt()

Dim regex As Object

Dim regMatch As Object

Set regex = CreateObject("VBScript.RegExp")

Dim str As String

str = Selection.Text

With regex

.Pattern = "(\d),"

.MultiLine = True

.Global = True

.IgnoreCase = True

str = .Replace(str, "$1 ")

Debug.Print (str)

End With

Selection.Text = str

End Sub

-End-

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接