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

如题所述

引用了Microsoft VBScript Regular Expressions 5.5 后就可以声明正则相关对象了。主要有三个对象:RegExp、MatchCollection、Match。
  1. RegExp 这是VB使用正则表达式匹配模式的主要对象了。其提供的属性用于设置那些用来比较的传递给 RegExp 实例的字符串的模式。 其提供的方法以确定字符串是否与正则表达式的特定模式相匹配。
  属性:
  Pattern:一个字符串,用来定义正则表达式。
  IgnoreCase:,则忽略英文字母大小的匹配,False对大小写进行匹配。
  Global:设置一个布尔值或返回一个布尔值,该布尔值指示一个模式是必须匹配整个搜索字符串中的所有搜索项还是只匹配第一个搜索项。
  MultiLine:这个MS没有介绍。查了一下资料,设置一个布尔值或返回一个布尔值,是否在串的多行中搜索。如果允许匹配多行文本,则multiline为true,如果搜索必须在换行时停止,则为false 。
  方法:
  Execute:返回一个 MatchCollection 对象,该对象包含每个成功匹配的 Match 对象。
  Replace:MS没有介绍,这是返回一个将匹配字符替换为指定字符的字符串。
  Test:返回一个布尔值,该值指示正则表达式是否与字符串成功匹配。
  2. MatchCollection 是集合对象,包含有关匹配字符串的信息,该对象包含每个成功匹配的 Match 对象。
  属性
  Count:匹配对象的总数。
  Item:匹配对象的索引。
  3. Match 是成功匹配的对象。
  属性:
  FirstIndex:匹配对象所匹配字符串的起始位置。
  Length:匹配对象所匹配字符串的字符长度。
  SubMatches:匹配对象所匹配结果的子项。
  Value:匹配对象所匹配的值。
见下面的几个简单示例:
1.匹配过程
Function TestRegExp(myPattern As String, myString As String)
   Dim objRegExp As RegExp                      '定义对象
   Dim objMatch As Match
   Dim colMatches   As MatchCollection          '对象包含有关匹配字符串的信息
   Dim RetStr As String
   Set objRegExp = New RegExp
   objRegExp.Pattern = myPattern                '传入参数,用来定义正则表达式
   objRegExp.IgnoreCase = True
   objRegExp.Global = True
   If objRegExp.Test(myString) Then    '正则表达式与字符串成功匹配
    Set colMatches = objRegExp.Execute(myString) 
    For Each objMatch In colMatches
      RetStr = RetStr & "发现位置" & objMatch.FirstIndex & ". 匹配值是 '" & objMatch.Value & "'." & vbCrLf
    Next
   Else
    RetStr = "String Matching Failed"
   End If
   TestRegExp = RetStr
End Function
Private Sub Command1_Click()
    MsgBox (TestRegExp("is.", "ISss1 is2 IS3 is4"))
End Sub
2. RegExp的Test方法:
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 = "我的邮箱: [email protected] 。欢迎致电!"
'测试字符串中是否包含数字:
 p = "\d+"
MsgBox bTest(s, p)
'测试字符串中是否全是由数字组成:
 p = "^\d+$"
MsgBox bTest(s, p)
'测试字符串中是否有大写字母:
 p = "[A-Z]+"
MsgBox bTest(s, p)
End Sub
3. 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: [email protected] 。欢迎致电!"
 p = "w+@w+.w+"
 r = "[email protected]"
 s = StrReplace(s, p, r)
 Debug.Print s
'结果:我的E-mail: [email protected] 。欢迎致电!
End Sub
4. 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: [email protected] 。欢迎致电!"
Set re = New RegExp
re.Pattern = "(w+)@(w+).(w+)"'同样是匹配地址,注意和上例的不同
Set mhs = re.Execute(inpStr)
Set mh = mhs(0)'只有一个匹配
 Print "电子邮件地址是: " & mh.Value'这里是匹配的内容
 Print "用户名是:" & mh.SubMatches(0)'第一个括号中的内容
 Print "邮箱是:" & mh.SubMatches(1)'第二个括号中的内容
 Print "域名是:  " & mh.SubMatches(2)'第三个括号中的内容

温馨提示:答案为网友推荐,仅供参考
相似回答