您的位置:首页 > 编程语言 > VB

在VBA中使用正则表达式

2011-09-01 11:18 232 查看
 '引用了Microsoft VBScript Regular Expressions 5.5 后就可以声明正则相关对象了。主要有三个对象:RegExp、MatchCollection、Match。

  

    '1. RegExp 这是VB使用正则表达式匹配模式的主要对象了。其提供的属性用于设置那些用来比较的传递给 RegExp 实例的字符串的模式。 其提供的方法以确定字符串是否与正则表达式的特定模式相匹配。

    '属性:

    'Pattern:一个字符串,用来定义正则表达式。

    'IgnoreCase:一个布尔值属性,指示是否必须对一个字符串中的所有可能的匹配进行正则表达式测试。这是MS的解释,有点费解,实际使用中的实例是,如果True,则忽略英文字母大小的匹配,False对大小写进行匹配。

    'Global:设置一个布尔值或返回一个布尔值,该布尔值指示一个模式是必须匹配整个搜索字符串中的所有搜索项还是只匹配第一个搜索项。

    'MultiLine:这个MS没有介绍。查了一下资料,设置一个布尔值或返回一个布尔值,是否在串的多行中搜索。如果允许匹配多行文本,则multiline为true,如果搜索必须在换行时停止,则为false 。

    '方法:

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

    'Replace:MS没有介绍,这是返回一个将匹配字符替换为指定字符的字符串。

    'Test:返回一个布尔值,该值指示正则表达式是否与字符串成功匹配。

    '2. MatchCollection 是集合对象,包含有关匹配字符串的信息,该对象包含每个成功匹配的 Match 对象。

    '属性

    'Count:     匹配对象的总数?

    'Item:      匹配对象的索引?

    '3. Match 是成功匹配的对象。

    '属性:

    'FirstIndex: 匹配对象所匹配字符串的起始位置?

    'Length:     匹配对象所匹配字符串的字符长度?

    'SubMatches: 匹配对象所匹配结果的子项?

    'Value:      匹配对象所匹配的值?

Sub dd()

        Dim s As String

        Dim p As String

        Dim reg As RegExp 'VB使用正则表达式匹配模式的主要对象

        Dim mc  As MatchCollection '是集合对象,包含有关匹配字符串的信息,该对象包含每个成功匹配的 Match 对象

        Dim m   As Match '成功匹配的对象

        Dim sms As SubMatches '匹配对象所匹配结果的子项

        Dim i As Long

        

        s = "我女朋友的生日是:1983-11-12 "

        p = "([\d]{4})-([\d]{2})-([\d]{2}) "

        

        Set reg = New RegExp

        reg.Pattern = p 'pattern:一个字符串,用来定义正则表达式

        Set mc = reg.Execute(s) 'Execute:返回一个 MatchCollection 对象,该对象包含每个成功匹配的 Match 对象

        

        For Each m In mc

                MsgBox m.Value

               

                Set sms = m.SubMatches

                For i = 0 To sms.Count - 1

                        MsgBox sms.Item(i)

                Next i

                Set sms = Nothing

        Next m

        Set mc = Nothing

        

        Set reg = Nothing

        

End Sub

Function bTest(ByVal s As String, ByVal p As String) As Boolean

    Dim re As RegExp

    Set re = New RegExp

    re.IgnoreCase = False  '设置是否匹配大小写

    re.Pattern = p

    bTest = re.Test(s)

End Function

Private Sub Command1_Click()

    Dim s As String

    Dim p As String

        

    s = "我的邮箱: test@163.com 。欢迎致电!"

    '测试字符串中是否包含数字:

    p = "\d+"

    MsgBox bTest(s, p)

    '测试字符串中是否全是由数字组成:

    p = "^\d+$"

    MsgBox bTest(s, p)

    '测试字符串中是否有大写字母:

    p = "[A-Z]+"

    MsgBox bTest(s, p)

   

End Sub

Function StrReplace(s As String, p As String, r As String) As String

   

    Dim re As RegExp

    Set re = New RegExp

    re.IgnoreCase = True

    re.Global = True

    re.Pattern = p

    StrReplace = re.Replace(s, r)

   

End Function

Private Sub Command2_Click()

    Dim s As String     '字符串

    Dim p As String     '正则表达式

    Dim r As String     '要替换的字符串

    '以下代码是替换邮箱地址

     

    s = "我的E-mail: Test@163.com 。欢迎致电!"

    p = "\w+[\w.]*@[\w.]+\.\w+"

    r = "E_Mail@sohu.net"

    s = StrReplace(s, p, r)

    Debug.Print s

    '结果:我的E-mail: E_Mail@sohu.net 。欢迎致电!

End Sub

Private Sub Command3_Click()

    Dim re As RegExp

    Dim mh As Match

    Dim mhs As MatchCollection

    Dim inpStr As String

   

    inpStr = "我的E-mail: lucky@163.com 。欢迎致电!"

    Set re = New RegExp

    re.Pattern = "(\w+)@(\w+).(\w+)"         '同样是匹配地址,注意和上例的不同

    Set mhs = re.Execute(inpStr)

    Set mh = mhs(0)                          '只有一个匹配

   

    Debug.Print "电子邮件地址是: " & mh.Value                '这里是匹配的内容

    Debug.Print "用户名是:             " & mh.SubMatches(0)  '第一个括号中的内容

    Debug.Print "邮箱是:                 " & mh.SubMatches(1)  '第二个括号中的内容

    Debug.Print "域名是:           " & mh.SubMatches(2)  '第三个括号中的内容

   

End Sub

转帖http://club.excelhome.net/thread-672121-1-1.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息