本次示例效果如下:

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

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

  1. PHP 中替换若干字符串字串为数组中的值,不用循环,非常高效

    替换某个字符串中的一个或若干个字串为数组中某些值 php本身有自带的函数,可以不用循环非常高效的实现其效果: 实例代码:   $phrase  = "You should eat fruit ...

  2. javascript replace正则替换时调用函数替换的使用

    以下函数将替换英文方式下的单引号和双引号,当然change函数编写决定了你要替换什么? String.prototype.repSpecChar=function()      {           ...

  3. javascript string replace 正则替换

    利用正则式实现首字母大写,丧心病狂是不是?好好的substr不用. JavaScript replace() 方法 r = /^(.)(?=.*)/; str = 'abc'; var str2 = ...

  4. Java replace() 方法 替换字符 也可用于 js 中

    JAVA中: public class Test { public static void main(String args[]) { String Str = new String("he ...

  5. js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式

    js replace 全局替换   js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <scrip ...

  6. Regex 正则替换指定范围全部字符串

    1.正则替换字符-------------------------------------------------------------------------------------------- ...

  7. 正则替换replace中$1的用法以及常用正则

    一.repalce定义 用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. stringObject.replace(regexp/substr,replacement)参数一 ...

  8. 正则替换replace中$1的用法

    一.repalce定义 用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 1 2 3 4 5 stringObject.replace(regexp/substr,repla ...

  9. 缓存需要注意的问题以及使用.net正则替换字符串的方法

    参考资料:http://www.infoq.com/cn/news/2015/09/cache-problems 正则替换字符串的简单方法: var regTableType = new Regex( ...

随机推荐

  1. JavaScript,通过分析Array.prototype.push重新认识Array

    在阅读ECMAScript的文档的时候,有注意到它说,数组的push方法其实不仅限于在数组中使用,专门留作通用方法.难道是说,在一些类数组的地方也可以使用?而哪些是和数组非常相像的呢,大家或许一下子就 ...

  2. UVALive 7275 Dice Cup (水题)

    Dice Cup 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/D Description In many table-top ...

  3. codeforces 653A Bear and Three Balls

    A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. Linux vmstat:报告虚拟内存统计的工具

    众所周知,计算机必须有称之为RAM(随机访问内存)的存储器使得计算机工作.RAM指的是插在计算机主板上的物理存储.这里的RAM被用于加载像浏览器.文字处理器这类的程序,实际上,你使用的程序都运行在内存 ...

  5. UVaLive 7371 Triangle (水题,判矩形)

    题意:给定两个三角形,问你能不能拼成矩形. 析:很明显,要想是矩形,必须是四个角是直角,那么三角形必须是直角三角形,然后就是只能斜边相对,然后呢?就没了. 代码如下: #pragma comment( ...

  6. AxWindowsMediaPlayer创建、添加播放列表(C#)

    // 创见打开对话框对象实例            OpenFileDialog openFileDialog = new OpenFileDialog(); //设置为可以打开多个文件        ...

  7. JS与Jquery的事件委托——解决了绑定相同事件的问题

    概念: 什么是事件委托:通俗的讲,事件就是onclick,onmouseover,onmouseout,等就是事件,委托呢,就是让别人来做,这个事件本来是加在某些元素上的,然而你却加到别人身上来做,完 ...

  8. Spring REST实践之HATEOAS

    HATEOAS HATEOAS(The Hypermedia As The Engine Of Application Statue)是REST架构的主要约束."hepermedia&quo ...

  9. OS X Git连接github

    1. 运行到.local 2. cd ~/.ssh查看文件是否存在 3. ssh-keygen(创建public & private key) 4. 或者运行如下命令:cd ~/.ssh &a ...

  10. PostgreSQL的 initdb 源代码分析之四

    继续分析: if (pwprompt && pwfilename) { fprintf(stderr, _("%s: password prompt and password ...