匹配单层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 = @"&nbsp;<span style='color:#1F497D'><span>y<span></span>&nbsp; &nbsp;<span style='color:#1F497D;'>1</span>&nbsp;<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("&quot;", "'");
text = text.Replace("&nbsp;", "");
text = text.Replace("&lt;", "<");//将<的转义码&lt;都替换成<
text = text.Replace("&gt;", ">");//将>的转义码&gt;都替换成> //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();
}
}
}

最新文章

  1. 【vijos1900】 学姐吃寿司
  2. 字符数组char
  3. Three.js 3D特效学习
  4. cocopods的使用方法
  5. 203. Remove Linked List Elements
  6. 66.为什么有时候在ISE软件中,顶层文件不能置顶?
  7. QuickStart下的CommandFilter项目 github上自己修改过的版本
  8. 免费LInux主机资源
  9. HTTP/2 对 Web 性能的影响(下)
  10. NTP方式保证以时间戳同步可靠性
  11. 从SQL Server数据库转到Oracle数据库的数据脚本处理
  12. java学习笔记之字符流文件复制
  13. junit测试模板 unit-test
  14. zabbix 配置维护
  15. mysql 原理 ~ binlog
  16. navicat远程连接阿里云ECS上的MYSQL报Lost connection to MySQL server at &#39;reading initial communication packet&#39;
  17. HDU 2036 叉乘求三角形面积
  18. [ 记录 ] Vue 对象数组中一项数据改变,页面不更新
  19. 分布式理论:深入浅出Paxos算法
  20. v-if案例解析(element-ui form-item 结合 v-if 动态生成rule规则\表单元素,表单无法验证问题剖析 )

热门文章

  1. Masonry remake更新约束
  2. 【转】js中15个常用的正则表达式+正则集合
  3. Combo Box (组合框)控件的使用方法
  4. Linux 常用命令二 pwd cd
  5. bzoj 1084: [SCOI2005]最大子矩阵【dp】
  6. Golang bash alias 自动配置GOPATH并运行项目
  7. 《Windows核心编程系列》二十谈谈DLL高级技术
  8. bzoj2581 [USACO 2012 Jan Gold] Cow Run【And-Or Tree】
  9. Oracle代码 规则 创建表 表空间 用户等
  10. mysql 中 时间函数 now() current_timestamp() 和 sysdate() 比较