C#解析单层html的中的文本,然后拼接起来
2024-09-30 17:43:16
匹配单层html的小demo,应该能匹配大多数html字符串.多层(嵌套)html标签解析不出来.可能有小bug,我抛砖引玉下,哈哈.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; namespace ResolveHtmlText
{
class Program
{
static void Main(string[] args)
{
string text = @" <span style='color:#1F497D'><span>y<span></span> <span style='color:#1F497D;'>1</span> <span style='color:#1F497D;background-color:#123456'>2</span><span style='color:#1F497D;background-color:#123456;text-align:center'>3</span> <span style='color:#1F497D;background-color:#123456;text-align:center;'>4</span> <span style='color:#1F497D;background-color:#123456;text-align:center;tt-l: 134;'>5</span>ggjf<a>123456</a>";
Console.WriteLine("原字符串:" + text);
text = text.Replace("\"", "'");
text = text.Replace(""", "'");
text = text.Replace(" ", "");
text = text.Replace("<", "<");//将<的转义码<都替换成<
text = text.Replace(">", ">");//将>的转义码>都替换成> //string matchStr = @"<\s*[a-zA-Z0-9]+\s*>[^<^>]*<\s*/\s*[a-zA-Z0-9]+\s*>";
string matchStr = @"<\s*[a-zA-Z0-9]+\s*[a-zA-Z]+\s*=\s*'\s*[a-zA-Z]"
+ @"+\s*:\s*[^<^>];?'\s*>[^<^>]"
+ @"*<\s*/\s*[a-zA-Z0-9]+\s*>|<\s*[a-zA-Z0-9]"
+ @"+\s*(\s*[a-zA-Z-]+\s*=\s*'(\s*[a-zA-Z-]+\s*:"
+ @"\s*[^:^;^<^>]+\s*;\s*)*(\s*[a-zA-Z-]+\s*:\s*"
+ @"[^:^;^<^>]+\s*)\s*;?\s*'\s*)*"
+ @"\s*>[^<^>]*<\s*/\s*[a-zA-Z0-9]+\s*>"; Regex htmlReg = new Regex(matchStr);
string result = null;
MatchCollection htmlMatchCollection = htmlReg.Matches(text);
StringBuilder sb = new StringBuilder(); foreach (Match m in htmlMatchCollection)
{
if (m != null && m.Groups != null && m.Groups.Count > )
{
string temp = m.Groups[].Value;
Console.WriteLine("临时值:" + temp);
//Regex textReg1 = new Regex(@"[^<^>]+");
//Match textMatch1 = textReg1.Match(temp);
//if (textMatch1 != null && textMatch1.Groups != null && textMatch1.Groups.Count > 0)
//{
// result = textMatch1.Groups[0].Value;
// sb.Append(result);
//}
Regex textReg = new Regex(@">.+<");
Match textMatch = textReg.Match(temp);
if (textMatch != null && textMatch.Groups != null && textMatch.Groups.Count > )
{
result = textMatch.Groups[].Value;
if (result.Length > )
{
result = result.Substring(, result.Length - );
sb.Append(result);
}
}
}
} Console.WriteLine("解析出的结果:" + sb.ToString());
Console.ReadLine();
}
}
}
最新文章
- 【vijos1900】 学姐吃寿司
- 字符数组char
- Three.js 3D特效学习
- cocopods的使用方法
- 203. Remove Linked List Elements
- 66.为什么有时候在ISE软件中,顶层文件不能置顶?
- QuickStart下的CommandFilter项目 github上自己修改过的版本
- 免费LInux主机资源
- HTTP/2 对 Web 性能的影响(下)
- NTP方式保证以时间戳同步可靠性
- 从SQL Server数据库转到Oracle数据库的数据脚本处理
- java学习笔记之字符流文件复制
- junit测试模板 unit-test
- zabbix 配置维护
- mysql 原理 ~ binlog
- navicat远程连接阿里云ECS上的MYSQL报Lost connection to MySQL server at &#39;reading initial communication packet&#39;
- HDU 2036 叉乘求三角形面积
- [ 记录 ] Vue 对象数组中一项数据改变,页面不更新
- 分布式理论:深入浅出Paxos算法
- v-if案例解析(element-ui form-item 结合 v-if 动态生成rule规则\表单元素,表单无法验证问题剖析 )
热门文章
- Masonry remake更新约束
- 【转】js中15个常用的正则表达式+正则集合
- Combo Box (组合框)控件的使用方法
- Linux 常用命令二 pwd cd
- bzoj 1084: [SCOI2005]最大子矩阵【dp】
- Golang bash alias 自动配置GOPATH并运行项目
- 《Windows核心编程系列》二十谈谈DLL高级技术
- bzoj2581 [USACO 2012 Jan Gold] Cow Run【And-Or Tree】
- Oracle代码 规则 创建表 表空间 用户等
- mysql 中 时间函数 now() current_timestamp() 和 sysdate() 比较