VB通过正则表达式定位字符串

2026-03-23
VB 通过正则表达式定位字符串的实现方法如下:
  [工程]-[引用] \"Microsoft VBScript Regular Expressions\"
  ’本例是要定位字符串s中数字出现的位置
  Private Sub Command1_Click()
  Dim re As RegExp, s As String, Matchs
  s = \"http://blog.sina.com.cn/juyonghong/123456.html\"
  Set re = New RegExp
  re.Pattern = \"\\d+\" ’数字序列
  If re.Test(s) Then ’测试是否存在
  Set Matchs = re.Execute(s)
  Print Matchs(0).FirstIndex ’显示位置
  End If
  End Sub