vb.net 使用 Regex Replace 正则 替换 Html字串的table中tbody第一个tr下的td为th

时间:2022-06-02 22:28:39

本次示例效果如下:

TextBox1中输入如下字符串:

12<table><tbody><tr><td>1<br/>11</td><td>a</td></tr><tr><td>2</td><td>b</td></tr></tbody></table>

34<table><tbody><tr><td>3</td><td>c</td></tr><tr><td>4</td><td>d</td></tr></tbody></table>

点击按钮, 转换第一个tr下面的td为tr 将TextBox1中的table中第一个tr下面的td 转换成th ,将结果显示在TextBox2中

12<table><tbody><tr><th>1<br/>11</th><th>a</th></tr><tr><td>2</td><td>b</td></tr></tbody></table>

34<table><tbody><tr><th>3</th><th>c</th></tr><tr><td>4</td><td>d</td></tr></tbody></table>

转换第一个tr下面的tr为td 将TextBox2中的table中第一个tr下面的th 转换成td ,将结果显示在TextBox3中

12<table><tbody><tr><th>1<br/>11</th><th>a</th></tr><tr><td>2</td><td>b</td></tr></tbody></table>

34<table><tbody><tr><th>3</th><th>c</th></tr><tr><td>4</td><td>d</td></tr></tbody></table>

相关代码如下:

Imports System.Text.RegularExpressions 

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox2.Text = ConvertFirstTrTdToTh(TextBox1.Text.Trim)

        TextBox3.Text = ConvertFirstTrThToTd(TextBox2.Text.Trim)

    End Sub

   Public Function ConvertFirstTrTdToTh(ByVal strContent As String) As String        

   Dim strOld As String = strContent.Trim

        'RegexOptions.Singleline 模式下 .代替任何字符 包括回车换行        

  Dim regexTable As New Regex("<tbody[^>]*>(.+?)</tr>", RegexOptions.Singleline) 

  Dim regexFirstTr As New Regex("<tr[^>]*>(.+?)</tr>", RegexOptions.Singleline)        

  Dim regexTd As New Regex("<td[^>]*>(?<tdContent>.+?)</td>", RegexOptions.Singleline)

        For Each mTable As Match In regexTable.Matches(strOld)

            Dim strFirstTr As String = regexFirstTr.Match(mTable.Value).Value

            'For Each mTd As Match In regexTd.Matches(strFirstTr)            

    Dim strTdTh As String = regexTd.Replace(strFirstTr, "<th>${tdContent}</th>")            

    'Next

            strOld = New Regex(strFirstTr).Replace(strOld, strTdTh, )

        Next

        ConvertFirstTrTdToTh = strOld     

End Function

    Public Function ConvertFirstTrThToTd(ByVal strContent As String) As String        

  Dim strOld As String = strContent.Trim

      Dim regexTable As New Regex("<tbody[^>]*>(.+?)</tr>", RegexOptions.Singleline) 

  Dim regexFirstTr As New Regex("<tr[^>]*>(.+?)</tr>", RegexOptions.Singleline)        

  Dim regexTd As New Regex("<th[^>]*>(?<tdContent>.+?)</th>", RegexOptions.Singleline)

        For Each mTable As Match In regexTable.Matches(strOld)

            'Dim mgFirstTr As MatchCollection = regexFirstTr.Matches(mTable.Value)

            Dim strFirstTr As String = regexFirstTr.Match(mTable.Value).Value

            'For Each mTd As Match In regexTd.Matches(strFirstTr)            

Dim strTdTh As String = regexTd.Replace(strFirstTr, "<td>${tdContent}</td>")           

  'Next

            strOld = New Regex(strFirstTr).Replace(strOld, strTdTh, )

        Next

        ConvertFirstTrThToTd = strOld    

 End Function

End Class