曾经的方法是使用正则表达式。另一种方法是使用 Split 在各种分隔符上拆分字符串,例如
Option Explicit
Sub splitMethod()
Dim Str As String
Str = Sheet1.Range("A1").Value
Debug.Print Split(Str, """")(1)
Debug.Print Split(Split(Str, ">")(1), "</a")(0)
End Sub
Sub RegexMethod()
Dim Str As String
Dim oRegex As Object
Dim regexArr As Object
Dim rItem As Object
'Assumes Sheet1.Range("A1").Value holds example string
Str = Sheet1.Range("A1").Value
Set oRegex = CreateObject("vbscript.regexp")
With oRegex
.Global = True
.Pattern = "(href=""|>)(.+?)(""|</a>)"
Set regexArr = .Execute(Str)
'No lookbehind so replace unwanted chars
.Pattern = "(href=""|>|""|</a>)"
For Each rItem In regexArr
Debug.Print .Replace(rItem, vbNullString)
Next rItem
End With
End Sub
'Output:
'http://www.nbc.com/xyz
'I love this show
这匹配href="
或>
在字符串的开头,"
或</a>
在字符串的末尾,中间有任何字符(\n 换行符除外) (.+?)