Link to home
Start Free TrialLog in
Avatar of clintnash
clintnashFlag for United States of America

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of clintnash

ASKER

Brilliant! Thank you!
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