如何在word中使用VBA(MACRO)使用/启用(RegExp对象)正则表达式 [英] How to Use/Enable (RegExp object) Regular Expression using VBA (MACRO) in word

查看:77
本文介绍了如何在word中使用VBA(MACRO)使用/启用(RegExp对象)正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了很多谷歌搜索以获得关于如何在 VBA 中使用或开始使用正则表达式的正确答案.

我终于明白了,所以我想与你们分享我的知识.如果我错了,请纠正我.

解决方案

Word 2007 中默认正则表达式选项是禁用的,要启用请执行以下步骤,

1).转到工具> 参考,如下所示.

2).现在勾选Microsoft VBScript 正则表达式 5.5"选项,然后按 oh ,如下所示.

3).现在,您可以在 VBA 脚本中创建一个 RegExp 对象.您可以验证它是否在对象数据库中搜索,如下所述.查看>对象浏览器(或按F2),如下图

并搜索 RegExp 对象

4).RegExp 对象使用正则表达式来匹配模式.以下属性由 RegExp 提供.这些属性设置模式来比较传递给 RegExp 实例的字符串:

a. 模式: 定义正则表达式的字符串.

b. IgnoreCase: 一个布尔属性,指示您是否必须针对字符串中的所有可能匹配项测试正则表达式.>

c. 全局: 设置一个布尔值或返回一个布尔值,指示一个模式是否必须匹配整个中的所有出现搜索字符串,或者模式是否必须只匹配第一次出现.

RegExp 提供以下方法来确定字符串是否与正则表达式的特定模式匹配:

d. 测试: 返回一个布尔值,指示正则表达式是否可以成功与字符串匹配.

e. 执行: 返回一个 MatchCollection 对象,该对象包含每个成功匹配的 Match 对象.

请在 Microsoft msdn 论坛中找到 RexExp 的明喻示例.

函数 TestRegExp(myPattern As String, myString As String)'创建对象.Dim objRegExp 作为 RegExpDim objMatch As MatchDim colMatches As MatchCollectionDim RetStr 作为字符串' 创建一个正则表达式对象.设置 objRegExp = 新的 RegExp'使用 Pattern 属性设置模式.objRegExp.Pattern = myPattern' 设置不区分大小写.objRegExp.IgnoreCase = True'设置全球适用性.objRegExp.Global = True'测试是否可以比较字符串.如果 (objRegExp.Test(myString) = True) 然后'得到火柴.Set colMatches = objRegExp.Execute(myString) ' 执行搜索.For Each objMatch In colMatches ' Iterate Matches 集合.RetStr = RetStr &在位置找到匹配"RetStr = RetStr &objMatch.FirstIndex &". 匹配值为 '"RetStr = RetStr &objMatch.Value &'."&vbCrLf下一个别的RetStr = "字符串匹配失败"万一TestRegExp = RetStr结束函数

我希望它对某些人有帮助,因为我浪费了将近半天时间.

谢谢

I did lot of Googling to get a proper answer on how to use or start using Regular Expressions in VBA.

At last I got it so I'd like to share my knowledge with you guys. Please correct me if I am wrong.

解决方案

By default Regular Expression option is disabled in word 2007, to enable that plase do following steps,

1). Go to Tools > References as shown below.

2). Now put a tick on "Microsoft VBScript Regular Expressions 5.5" option and then press oh as shown below.

3). Now onward you can create a RegExp object in your VBA script. You can verify it be searching in object data base as explained below. View > Object Browser ( Or press F2) , as shown below.

and search for RegExp object

4). The RegExp object uses regular expressions to match a pattern. The following properties are provided by RegExp. These properties set the pattern to compare the strings that are passed to the RegExp instance:

a. Pattern: A string that defines the regular expression.

b. IgnoreCase: A Boolean property that indicates whether you must test the regular expression against all possible matches in a string.

c. Global: Sets a Boolean value or returns a Boolean value that indicates whether a pattern must match all the occurrences in a whole search string, or whether a pattern must match just the first occurrence.

RegExp provides the following methods to determine whether a string matches a particular pattern of a regular expression:

d. Test: Returns a Boolean value that indicates whether the regular expression can successfully be matched against the string.

e. Execute: Returns a MatchCollection object that contains a Match object for each successful match.

Please find a simile example for RexExp provided in Microsoft msdn forum.

Function TestRegExp(myPattern As String, myString As String)
   'Create objects.
   Dim objRegExp As RegExp
   Dim objMatch As Match
   Dim colMatches   As MatchCollection
   Dim RetStr As String

   ' Create a regular expression object.
   Set objRegExp = New RegExp

   'Set the pattern by using the Pattern property.
   objRegExp.Pattern = myPattern

   ' Set Case Insensitivity.
   objRegExp.IgnoreCase = True

   'Set global applicability.
   objRegExp.Global = True

   'Test whether the String can be compared.
   If (objRegExp.Test(myString) = True) Then

   'Get the matches.
    Set colMatches = objRegExp.Execute(myString)   ' Execute search.

    For Each objMatch In colMatches   ' Iterate Matches collection.
      RetStr = RetStr & "Match found at position "
      RetStr = RetStr & objMatch.FirstIndex & ". Match Value is '"
      RetStr = RetStr & objMatch.Value & "'." & vbCrLf
    Next
   Else
    RetStr = "String Matching Failed"
   End If
   TestRegExp = RetStr
End Function

I hope it might help full for some one, because i wasted almost half a day on it.

Thanks

这篇关于如何在word中使用VBA(MACRO)使用/启用(RegExp对象)正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆