Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get value between two tags 1

Status
Not open for further replies.

guitardave78

Programmer
Sep 5, 2001
1,294
GB
Hey all.
I have the following string

<postcode>1xxxxxxx1</postcode><postcode>2xxxxxx2</postcode>

I want to find the 1xxxx1 and the 2xxxx2 and turn them into google map links
How do i find the value between the tags?

}...the bane of my life!
 
Is this just an example small piece of a big string or will there only ever be the two?

If it is only a small simple thing like this you could use Instr() with Mid() to pull it out...

but if it is a big XML page you would want to consider loading it up into the xml parser or using a regular expression.
 
it will look somthing like this

asd asa sdas sa as das<postcode>asd</postcode>
asd asd as<postcode>asdasda</postcode>aa sasd
as
das dasd sd as d
asd

}...the bane of my life!
 
Not sure how great a way this is, but it works :)

Code:
function customTags(str)
	Dim objRegExp
	Set objRegExp = New RegExp
	tag = "postcode"
	objRegExp.Pattern = "<"&tag&">(.*?)<\/"&tag&">"
	objRegExp.IgnoreCase = True
	objRegExp.Global = True
	Dim objMatches
	Set objMatches = objRegExp.Execute(str)

	Dim objMatch
	For Each objMatch in objMatches
		m = Mid(objMatch.Value, len(tag) + 3, Len(objMatch.Value) - ((len(tag)+3)*2)+1)
		m = "<a href='[URL unfurl="true"]http://maps.google.com/maps?q="&m&"'[/URL] title='Google Map'>"&m&"</a>"
		str = replace(str,objMatch.Value,m)
	Next
	customTags = str
	set objRegExp = nothing
end function

}...the bane of my life!
 
Ok, thats close to how I would have done it, but I would have used the submatches of the match to get the value, rather than parsing the string (why do more work if the RegExp already did it? ;) )

Something like:
Code:
Function customTags(str)
    Dim objRegExp
    Set objRegExp = New RegExp
    objRegExp.Pattern = "<postcode>(.*?)<\/postcode>"
    objRegExp.IgnoreCase = True
    objRegExp.Global = True
    Dim objMatches
    Set objMatches = objRegExp.Execute(str)

    Dim objMatch
    For Each objMatch in objMatches
        m = objMatch.SubMatches(0)
        m = "<a href='[URL unfurl="true"]http://maps.google.com/maps?q="&m&"'[/URL] title='Google Map'>"&m&"</a>"
        str = replace(str,objMatch.Value,m)
    Next
    customTags = str
    set objRegExp = nothing
End Function

I also made the postcode portion directly part of the pattern string, simply because there was no way to set it outside the function and it was adding 4 concatenations to the execution stack.

I can't guarantee that my syntax is right on the SubMatches portion, but it should be close.

now if you instead wanted to return just the links (without the content that was originally around it) you could do something like this:
Code:
Function customTags(str)
    Dim objRegExp
    Dim result : result = ""
    ...
    ...
    For Each objMatch in objMatches
        m = objMatch.SubMatches(0)
        m = "<a href='[URL unfurl="true"]http://maps.google.com/maps?q="&m&"'[/URL] title='Google Map'>"&m&"</a>"
        If Len(result) > 0 The result = result & "###"
        result = result & m
    Next

    customTags = Split(result,"###")
    set objRegExp = nothing
End Function

Just another viewpoint,
-T

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top