Need to find the value between <tag> </tag> using VBscript

electricd7
electricd7 used Ask the Experts™
on
Hello,

I have using msxml to scrape the contents of a webpage into a variable (myvar).  Now I want to use VBscript to store the string between 2 specific tags found in the source.  The tags data I want is held between the following:

<div id="myscore">137475</div>

So I want to search the entire myvar variable and end up with a new variable that only holds the value "137475".  Can somone write a function that does this?  Seems like it would be fairly easy to do using InStr, but my brain is asleep this morning!

ED7
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010
Commented:
You could use Regular Expressions:

Function GetTagValue(LookIn)

    Dim RegX
    Dim Mats

    Set RegX = New RegExp
    With RegEx
        .Pattern = "(<div id=""myscore"">)([^<]+)(</div>)"
        .IgnoreCase = True
        If .Test(LookIn) Then
            Set Mats = RegX.Execute(LookIn)
            GetTagValue = Mats(0).Submatches(1)
            Set Mats = Nothing
        Else
            GetTagValue = ""
        End If
    End With

    Set RegX = Nothing

End Function

Open in new window



For more info on Regular Expressions, please see http://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html

Author

Commented:
I must not have RegExp installed?  I get an error on the line "With RegEx" that says:

Object required: 'RegEx'
Top Expert 2010

Commented:
Sorry, typo.  Change:

    With RegEx

Open in new window


to:

    With RegX

Open in new window

Author

Commented:
Perfect, thanks!
Top Expert 2010

Commented:
electricd7,

Glad to help!  If you have not already done so, I would really appreciate it if you could please return to my article
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
and click 'Yes' for the 'Was this helpful?' voting.

Patrick

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial