如何在单词中使用/启用(RegExp对象)使用VBA(MACRO)的正则表达式

时间:2023-01-14 23:54:15

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

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

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

最后我明白了,所以我想和你们分享我的知识。如果我错了,请纠正我。

1 个解决方案

#1


30  

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

默认情况下,在Word 2007中禁用正则表达式选项,以启用该步骤执行以下步骤,

1). Go to Tools > References as shown below. 如何在单词中使用/启用(RegExp对象)使用VBA(MACRO)的正则表达式

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

2). Now put a tick on "Microsoft VBScript Regular Expressions 5.5" option and then press oh as shown below. 如何在单词中使用/启用(RegExp对象)使用VBA(MACRO)的正则表达式

2)。现在勾选“Microsoft VBScript Regular Expressions 5.5”选项,然后按哦,如下所示。

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.

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

如何在单词中使用/启用(RegExp对象)使用VBA(MACRO)的正则表达式

and search for RegExp object

并搜索RegExp对象

如何在单词中使用/启用(RegExp对象)使用VBA(MACRO)的正则表达式

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:

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

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.

湾IgnoreCase:一个Boolean属性,指示是否必须针对字符串中的所有可能匹配测试正则表达式。

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.

C。全局:设置布尔值或返回一个布尔值,该值指示模式是否必须匹配整个搜索字符串中的所有匹配项,或者模式是否必须仅匹配第一个匹配项。

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

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

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

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

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

即Execute:返回一个MatchCollection对象,该对象包含每个成功匹配的Match对象。

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

请在Microsoft msdn论坛中找到RexExp的一个明喻示例。

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

谢谢

#1


30  

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

默认情况下,在Word 2007中禁用正则表达式选项,以启用该步骤执行以下步骤,

1). Go to Tools > References as shown below. 如何在单词中使用/启用(RegExp对象)使用VBA(MACRO)的正则表达式

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

2). Now put a tick on "Microsoft VBScript Regular Expressions 5.5" option and then press oh as shown below. 如何在单词中使用/启用(RegExp对象)使用VBA(MACRO)的正则表达式

2)。现在勾选“Microsoft VBScript Regular Expressions 5.5”选项,然后按哦,如下所示。

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.

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

如何在单词中使用/启用(RegExp对象)使用VBA(MACRO)的正则表达式

and search for RegExp object

并搜索RegExp对象

如何在单词中使用/启用(RegExp对象)使用VBA(MACRO)的正则表达式

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:

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

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.

湾IgnoreCase:一个Boolean属性,指示是否必须针对字符串中的所有可能匹配测试正则表达式。

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.

C。全局:设置布尔值或返回一个布尔值,该值指示模式是否必须匹配整个搜索字符串中的所有匹配项,或者模式是否必须仅匹配第一个匹配项。

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

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

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

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

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

即Execute:返回一个MatchCollection对象,该对象包含每个成功匹配的Match对象。

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

请在Microsoft msdn论坛中找到RexExp的一个明喻示例。

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

谢谢