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


    本次示例效果如下:

    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, 1)
    
            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, 1)
    
            Next
    
            ConvertFirstTrThToTd = strOld    
    
     End Function
    
    End Class
  • 相关阅读:
    Atitit.ati  str  字符串增强api
    Atitit.加密算法 des  aes 各个语言不同的原理与解决方案java php c#
    Atitit.加密算法 des  aes 各个语言不同的原理与解决方案java php c#
    Atitit.html解析器的选型 jsoup nsoup ,java c# .net 版本
    Atitit.html解析器的选型 jsoup nsoup ,java c# .net 版本
    Atitit.软件开发的最终的设计 dsl化,ast化(建立ast, 解析执行ast)
    Atitit.软件开发的最终的设计 dsl化,ast化(建立ast, 解析执行ast)
    swfupload 上传控件的配置
    SWFUpload下载地址
    js 转化类似这样的时间( /Date(1389060261000)/)问题
  • 原文地址:https://www.cnblogs.com/freeliver54/p/3253136.html
Copyright © 2020-2023  润新知