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( ...
随机推荐
- Spring AOP Example – Advice
Spring AOP + AspectJ Using AspectJ is more flexible and powerful. Spring AOP (Aspect-oriented progra ...
- Mybatis中实体类中的字段跟对应表的字段不一致时解决办法
解决字段名与实体类属性名不相同的冲突 实体类字段: public class Order { private int id; private String orderNo; private float ...
- [iOS基础控件 - 7.0] UIWebView
A.基本使用 1.概念 iOS内置的浏览器控件 Safari浏览器就是通过UIWebView实现的 2.用途:制作简易浏览器 (1)基本请求 创建请求 加载请求 (2)代理监听webView加载, ...
- C#学习笔记(十):反射
反射 放射是指在程序运行时动态的获取类的信息的机制,我们下面来看看C#中的反射. Type Type 为 System.Reflection 功能的根,也是访问元数据的主要方式. 使用 Type 的成 ...
- 使用gcc编译gdb调试
gcc 在linux系统中,默认是没有安装gcc编译器的,可以通过命令 rpm -q | grep gcc 来查看.安装命令为: yum -y install gcc 安装后,编写一个.c结尾的文件. ...
- Codeforces Round #274 (Div. 1) B. Long Jumps 数学
B. Long Jumps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/ ...
- DOS命令生成文件列表
DOS命令窗口,生成文件列表命令格式:dir xmlFiles /b >list.txt dir 文件列表相关命令 xmlFiles 待生成文件所在文件夹,在dos命令窗口根目录下,省略前缀.别 ...
- Android监听SD卡文件变化
今天再一次使用到FileObserver,上一次使用还是很久之前了.总结一下FileObserver里留的一些“坑” 1.FileObserver只能监听一个目录下的“一级”子文件,也就是说Fil ...
- 【项目实例】android开发游戏音效代码实例
//音效的音量 int streamVolume; //定义SoundPool 对象 private SoundPool soundPool; //定义HASH表 private HashMap< ...
- orcle经常使用语句
--1.创建暂时表空间 create temporary tablespace AUTOMONITORV5_temp tempfile 'D:\ORACLE\KARL\ORADATA\ORCL\AUT ...