2012-10-04 30 views
2

我想知道是否有人能告訴我如何在Excel-VB中從以下字符串推斷'http://www.nbc.com/xyz'和'我喜歡這個節目'。如何在excel中操縱字符串 - VB?

感謝

<a href="http://www.nbc.com/xyz" >I love this show</a><IMG border=0 width=1 height=1 src="http://ad.linksynergy.com/fs-bin/show?id=Loe5O5QVFig&bids=261463.100016851&type=3&subid=0" > 

回答

1

一旦方法是使用正則表達式。另一種方式是使用分割到各個分隔符分割的字符串例如

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 

此匹配字符串,"</a>在與任何字符的字符串的末尾開始href=">(除了\ n換行)在之間(.+?)

5
Sub Tester() 
    '### add a reference to "Microsoft HTML Object Library" ### 
    Dim odoc As New MSHTML.HTMLDocument 
    Dim el As Object 
    Dim txt As String 

    txt = "<a href=""http://www.nbc.com/xyz"" >I love this show</a>" & _ 
     "<IMG border=0 width=1 height=1 " & _ 
     "src=""http://ad.linksynergy.com/fs-bin/show?" & _ 
     "id=Loe5O5QVFig&bids=261463.100016851&type=3&subid=0"" >" 

    odoc.body.innerHTML = txt 

    Set el = odoc.getElementsByTagName("a")(0) 
    Debug.Print el.innerText 
    Debug.Print el.href 

End Sub 
+0

這很酷! –

+1

不錯,但我可以在加載單個innerHTML的時間內處理至少1000個這樣的內容:) – user3357963

+0

@ooo - 好的,我沒有對性能做任何聲明!我傾向於喜歡這種方法,因爲它比正則表達式方法更寬容一些。例如,它將處理屬性,而不管它們是單引號還是雙引號。 –