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, ) 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的更多相关文章
- PHP 中替换若干字符串字串为数组中的值,不用循环,非常高效
替换某个字符串中的一个或若干个字串为数组中某些值 php本身有自带的函数,可以不用循环非常高效的实现其效果: 实例代码: $phrase = "You should eat fruit ...
- javascript replace正则替换时调用函数替换的使用
以下函数将替换英文方式下的单引号和双引号,当然change函数编写决定了你要替换什么? String.prototype.repSpecChar=function() { ...
- javascript string replace 正则替换
利用正则式实现首字母大写,丧心病狂是不是?好好的substr不用. JavaScript replace() 方法 r = /^(.)(?=.*)/; str = 'abc'; var str2 = ...
- Java replace() 方法 替换字符 也可用于 js 中
JAVA中: public class Test { public static void main(String args[]) { String Str = new String("he ...
- js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式
js replace 全局替换 js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <scrip ...
- Regex 正则替换指定范围全部字符串
1.正则替换字符-------------------------------------------------------------------------------------------- ...
- 正则替换replace中$1的用法以及常用正则
一.repalce定义 用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. stringObject.replace(regexp/substr,replacement)参数一 ...
- 正则替换replace中$1的用法
一.repalce定义 用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 1 2 3 4 5 stringObject.replace(regexp/substr,repla ...
- 缓存需要注意的问题以及使用.net正则替换字符串的方法
参考资料:http://www.infoq.com/cn/news/2015/09/cache-problems 正则替换字符串的简单方法: var regTableType = new Regex( ...
随机推荐
- homework-1
看到这个题目开始我只能想到动态规划四个字,但具体采用什么方法,如何写成代码却还未成型.动态规划的典型特点就是利用之前的结果.于是我很快想到了之前一个比较典型的小程序,即求最长的连续字符串.这两个题目有 ...
- ubuntu 备份安装程序列表
一般情况下,我们重装ubuntu的系统会做如下几个事情 1)修改默认的程序更新源 2)开始根据需求安装软件. 3)配置文件(如vim/tmux等) 对于步骤,只需要cp /etc//etc/apt/s ...
- 现在,UICollectionViews有了简单的重排功能
原文:UICollectionViews Now Have Easy Reordering 我是UICollectionView的忠实粉丝.这个类比起它的老哥UITableView类具有更高的可定制性 ...
- DelphiXE Android自适应屏幕办法
相关资料: http://www.delphitop.com/html/FireMonkey/2658.html http://bbs.csdn.net/topics/390919460 1.Scal ...
- POJ 3169 Layout (差分约束系统)
Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...
- 51单片机或PLC驱动3.5寸至52寸的数字TFTLCD屏、VGA接口显示器、电视机
http://www.21easyic.com/yx/VGA%E6%8E%A7%E5%88%B6%E6%9D%BF.htm
- Jeecms自定义标签用法[单个内容]
1.com.jeecms.cms.action.directive包下建立自己的标签类
- [Windows驱动开发](一)序言
笔者学习驱动编程是从两本书入门的.它们分别是<寒江独钓——内核安全编程>和<Windows驱动开发技术详解>.两本书分别从不同的角度介绍了驱动程序的制作方法. 在我理解,驱动程 ...
- OpenNebula 创建虚拟机失败(未解决)
Tue Jul :: [ReM][D]: Req: UID: AclInfo invoked Tue Jul :: [ReM][D]: Req: UID: AclInfo result SUCCESS ...
- Thinkphp框架----微信公众测试号开发
最开始的一个步骤.注册一个微信公众测试号.URL:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login; 接口配置的信息,对新手 ...