I have the following Regex that searches for a Tags h1, h2, ..., h5 and returns a match with group named TagName holding the Tag Name and group named TagValue holding the Tag Value.
Public Sub Main
Dim strSearched = <html>
<head>
<title>This is a test</title>
</head>
<body>
<h1>DA:TG01</h1>
<p>First paragraph</p>
<h2>This is a test 2</h2>
<!--More boring stuff omitted-->
</body>
</html>.ToString
Dim ResultString As String
Dim myMatchEvaluator As MatchEvaluator = New MatchEvaluator(AddressOf ComputeReplacement)
ResultString = Regex.Replace(strSearched,
"<(?'TagName'h[1-5])>(?'TagValue'.*?)</\k<TagName>>",
myMatchEvaluator,
RegexOptions.Singleline Or RegexOptions.IgnoreCase)
End Sub
Public Function ComputeReplacement(ByVal m As Match) As String
' Need to replace the Group('value') here
Return strRetValue
End Function
In the Function ComputeReplacement, I need to replace the Group("TagValue") with another value and return back the match string, eg:
If the match was <h1>AAA</h1>
I would need it to return <h1>BBB</h1>
while if the match was <h2>AAA</h2>
I would need it to return <h2>BBB</h2>