Replace text between tags in string vb.net

I have a large chunk of html data in a stringbuilder and need to modify the text between two tags. Through out the string are different sections (as follows)

<!-- BEGIN_DIVISION --> Cluster 23 <!--  
more code
<!-- BEGIN_DIVISION --> Cluster 24<!--
more code

I need to convert the text between the tags to include an anchor tag. For example
<!-- BEGIN_DIVISION --> <a name="Cluster 23"></a> <!--  
more code
<!-- BEGIN_DIVISION --> <a name="Cluster 24"></a><!--
more code

I have tried using indexof in a while loop and the first one works the rest get inserted into some unexpected and horribly incorrect places.
This is asp.net using vb.net code.
LVL 1
clintnashAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Perhaps something like this:
Function SetClusterAnchors(ByVal source As String) As String
	Dim index = source.IndexOf("BEGIN_DIVISION")
	Dim endTag As Integer
	Dim endCluster As Integer
	Dim preCluster As String = String.Empty
	Dim postCluster As String = String.Empty
	Dim Cluster As String = String.Empty

	While index <> -1
		endTag = source.IndexOf(">", index) + 1
		If endTag > 0 Then
			preCluster = source.Substring(0, endTag)
			endCluster = source.IndexOf("<", endTag)
			If endCluster <> -1 Then
				postCluster = source.Substring(endCluster)
				Cluster = source.Substring(endTag, endCluster - endTag)
				source = String.Format("{0}<a name=""{1}""></a>{2}", preCluster, Cluster.Trim(), postCluster)
			End If
		End If
		index = source.IndexOf("BEGIN_DIVISION", index + 1)
	End While
	Return source
End Function

Open in new window

Proof of concept -
Module Module1
	Private divisions As String = "<!-- BEGIN_DIVISION --> Cluster 23 <!-- TEXT_DIVISION -->" & _
	 "STUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFF" & _
	 "STUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFF"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FSTUFF" & _
	 "STUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFF" & _
	 "STUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFF" & _
	 "STUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFF" & _
	 "STUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFF" & _
	 "STUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFF" & _
	 "STUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFF" & _
	 "STUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFF" & _
	 "STUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFF" & _
	 "STUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFFSTUFF" & _
	 "<!-- BEGIN_DIVISION --> Cluster 27 <!-- TEXT_DIVISION -->"

	Sub Main()
		Console.WriteLine(SetClusterAnchors(divisions))
		Console.ReadLine()
	End Sub

	Function SetClusterAnchors(ByVal source As String) As String
		Dim index = source.IndexOf("BEGIN_DIVISION")
		Dim endTag As Integer
		Dim endCluster As Integer
		Dim preCluster As String = String.Empty
		Dim postCluster As String = String.Empty
		Dim Cluster As String = String.Empty

		While index <> -1
			endTag = source.IndexOf(">", index) + 1
			If endTag > 0 Then
				preCluster = source.Substring(0, endTag)
				endCluster = source.IndexOf("<", endTag)
				If endCluster <> -1 Then
					postCluster = source.Substring(endCluster)
					Cluster = source.Substring(endTag, endCluster - endTag)
					source = String.Format("{0}<a name=""{1}""></a>{2}", preCluster, Cluster.Trim(), postCluster)
				End If
			End If
			index = source.IndexOf("BEGIN_DIVISION", index + 1)
		End While
		Return source
	End Function
End Module

Open in new window

Produces the following output -Capture.JPG-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
clintnashAuthor Commented:
Brilliant! Thank you!
0
Miguel OzSoftware EngineerCommented:
You could use regex replace:
Dim matchCodeTag As String = "\-->(.*?)\<!--"
Dim textToReplace As String = "<!-- BEGIN_DIVISION --> Cluster 23 <!--" + "<!-- BEGIN_DIVISION --> Cluster 24<!--"
Dim replaceWith As String = "<a name=""{0}""></a>"
Return Regex.Replace(textToReplace, matchCodeTag, Function(m) [String].Format(replaceWith, m.Groups(1).Value.Trim()))

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.