我有以下正则表达式,它搜索标签 h1、h2、...、h5 并返回与包含标签名称的名为TagName的组和包含标签值的名为TagValue的组的匹配。
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
在 Function ComputeReplacement 中,我需要将 Group("TagValue") 替换为另一个值并返回匹配字符串,例如:
如果比赛是<h1>AAA</h1>
我需要它返回<h1>BBB</h1>
,而如果比赛是<h2>AAA</h2>
我需要它返回<h2>BBB</h2>