VB6.0如何使用正则表达式

最近在用VB6解决一个网页解析的应用,涉及到了正则。如何在VB6中使用正则表达式,这个在MS网站上有介绍,不过介绍的不是很全面。下面结合MS网站的内容和搜索到的东西,自己汇总一下。

  引用了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 :匹配对象所匹配的值。

  相关示例参照MS的网站:http://support.microsoft.com/kb/818802/zh-cn 。MS上没有介绍的几个属性和方法的使用,见下面的几个简单示例:1. RegExp的Test方法:

1. RegExp的Test方法:

Function bTest(ByVal s As String , ByVal p As String ) As Boolean
VB6.0如何使用正则表达式     
Dim re As RegExp
VB6.0如何使用正则表达式     
Set re = New RegExp
VB6.0如何使用正则表达式      re.IgnoreCase
= False   ' 设置是否匹配大小写
VB6.0如何使用正则表达式
     re.Pattern = p
VB6.0如何使用正则表达式      bTest
= re.Test(s)
VB6.0如何使用正则表达式
End Function
VB6.0如何使用正则表达式
VB6.0如何使用正则表达式
Private Sub Command1_Click()
VB6.0如何使用正则表达式
VB6.0如何使用正则表达式     
Dim s As String
VB6.0如何使用正则表达式     
Dim p As String
VB6.0如何使用正则表达式         
VB6.0如何使用正则表达式      s
= " 我的邮箱: test@163.com 。欢迎致电! "
VB6.0如何使用正则表达式
VB6.0如何使用正则表达式     
' 测试字符串中是否包含数字:
VB6.0如何使用正则表达式
     p = "\ d+ "
VB6.0如何使用正则表达式     
MsgBox bTest(s, p)
VB6.0如何使用正则表达式
VB6.0如何使用正则表达式     
' 测试字符串中是否全是由数字组成:
VB6.0如何使用正则表达式
     p = " ^\d+$ "
VB6.0如何使用正则表达式     
MsgBox bTest(s, p)
VB6.0如何使用正则表达式
VB6.0如何使用正则表达式     
' 测试字符串中是否有大写字母:
VB6.0如何使用正则表达式
     p = " [A-Z]+ "
VB6.0如何使用正则表达式     
MsgBox bTest(s, p)
VB6.0如何使用正则表达式     
VB6.0如何使用正则表达式
End Sub

2. RegExp的Replace方法:

Function StrReplace(s As String , p As String , r As String ) As String
VB6.0如何使用正则表达式     
VB6.0如何使用正则表达式     
Dim re As RegExp
VB6.0如何使用正则表达式     
Set re = New RegExp
VB6.0如何使用正则表达式      re.IgnoreCase
= True
VB6.0如何使用正则表达式      re.Global
= True
VB6.0如何使用正则表达式      re.Pattern
= p
VB6.0如何使用正则表达式      StrReplace
= re.Replace(s, r)
VB6.0如何使用正则表达式     
VB6.0如何使用正则表达式
End Function
VB6.0如何使用正则表达式
VB6.0如何使用正则表达式
Private Sub Command2_Click()
VB6.0如何使用正则表达式
VB6.0如何使用正则表达式     
Dim s As String       ' 字符串
VB6.0如何使用正则表达式
     Dim p As String       ' 正则表达式
VB6.0如何使用正则表达式
     Dim r As String       ' 要替换的字符串
VB6.0如何使用正则表达式

VB6.0如何使用正则表达式   
' 以下代码是替换邮箱地址
VB6.0如何使用正则表达式
     
VB6.0如何使用正则表达式      s
= " 我的E-mail: Test@163.com 。欢迎致电! "
VB6.0如何使用正则表达式      p
= " w+@w+.w+ "
VB6.0如何使用正则表达式      r
= " E_Mail@sohu.net "
VB6.0如何使用正则表达式      s
= StrReplace(s, p, r)
VB6.0如何使用正则表达式      Debug.Print s
VB6.0如何使用正则表达式     
' 结果:我的E-mail: E_Mail@sohu.net 。欢迎致电!
VB6.0如何使用正则表达式

VB6.0如何使用正则表达式
End Sub
3. Match的SubMatches属性:

Private Sub Command3_Click()
VB6.0如何使用正则表达式
VB6.0如何使用正则表达式     
Dim re As RegExp
VB6.0如何使用正则表达式     
Dim mh As Match
VB6.0如何使用正则表达式     
Dim mhs As MatchCollection
VB6.0如何使用正则表达式     
Dim inpStr As String
VB6.0如何使用正则表达式     
VB6.0如何使用正则表达式      inpStr
= " 我的E-mail: lucky@163.com 。欢迎致电! "
VB6.0如何使用正则表达式     
Set re = New RegExp
VB6.0如何使用正则表达式      re.Pattern
= " (w+)@(w+).(w+) "           ' 同样是匹配地址,注意和上例的不同
VB6.0如何使用正则表达式
     Set mhs = re.Execute(inpStr)
VB6.0如何使用正则表达式     
Set mh = mhs( 0 )                                       ' 只有一个匹配
VB6.0如何使用正则表达式
    
VB6.0如何使用正则表达式      Debug.Print
" 电子邮件地址是: " & mh.Value                 ' 这里是匹配的内容
VB6.0如何使用正则表达式
     Debug.Print " 用户名是:              " & mh.SubMatches( 0 )   ' 第一个括号中的内容
VB6.0如何使用正则表达式
     Debug.Print " 邮箱是:                  " & mh.SubMatches( 1 )   ' 第二个括号中的内容
VB6.0如何使用正则表达式
     Debug.Print " 域名是:            " & mh.SubMatches( 2 )   ' 第三个括号中的内容
VB6.0如何使用正则表达式
    
VB6.0如何使用正则表达式
End Sub

相关文章: