当前位置:编程学习 > VB >>

VB如何调用正则表达式

如题,例如我要找出某网页中的数字信息。
追问:这个代码在VB6.0中是错误的。能不能给我你的Q号?

答案:引用了Microsoft VBscrīpt 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:匹配对象所匹配的值。

RegExp的Test方法:

Private Sub Command1_Click()
Dim s As String
    Dim p As String
    s = Text1.Text
        '测试字符串中是否包含数字:
    p = "^13[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"

    MsgBox bTest(s, p)

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
RegExp的Replace方法:

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+"
    r = "E_Mail@sohu.net"
    s = StrReplace(s, p, r)
    Debug.Print s
    '结果:我的E-mail: E_Mail@sohu.net 。欢迎致电!

End Sub

  3. Match的SubMatches属性:

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 

 

步示例
1. 启动 Microsoft Visual Basic 6.0。
2. 在“文件”菜单上,单击“新建项目”。
3. 在“新建项目”对话框中,单击“Standard Exe”,然后单击“确定”。

默认情况下将创建 Form1。
4. 在“项目”菜单上单击“引用”。
5. 双击“Microsoft VBScript Regular Expressions 5.5”,然后单击“确定”。
6. 在工具箱中,双击“命令按钮”。

默认情况下,“Command1”将添加到窗体中。
7. 双击“Command1”以打开代码窗口。
8. 将下面的代码粘贴到“Command1_Click”事件处理程序:MsgBox(TestRegExp("is.", "IS1 is2 IS3 is4"))
注意 这个示例中将对照“IS1 is2 IS3 is4”字符串检查 is. 模式。您可以将句点这一特殊字符(.)用作通配符,这样,搜索模式就能够多匹配并多显示一个字符。如果您在搜索模式中添加两个句点,您会看到两个其他字符。如果您不使用任何句点,您只会看到搜索模式。
9. 将以下函数添加到“Command1_click”事件处理程序后: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
   &n

上一个:两道C++编程题,求高手解决
下一个:一份VB作业

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,