C#解析单层html的中的文本,然后拼接起来
匹配单层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();
}
}
}
C#解析单层html的中的文本,然后拼接起来的更多相关文章
- 关于ios中的文本操作-简介
来源:About Text Handling in iOS 官方文档 iOS平台为我们提供了许多在app中展示文本和让用户编辑文本的方式.同时,它也允许你在app视图中展示格式化的文本和网页内容.你可 ...
- java itext替换PDF中的文本
itext没有提供直接替换PDF文本的接口,我们可以通过在原有的文本区域覆盖一个遮挡层,再在上面加上文本来实现. 所需jar包: 1.先在PDF需要替换的位置覆盖一个白色遮挡层(颜色可根据PDF文字背 ...
- [译]使用BeautifulSoup和Python从网页中提取文本
如果您要花时间浏览网页,您可能遇到的一项任务就是从HTML中删除可见的文本内容. 如果您使用的是Python,我们可以使用BeautifulSoup来完成此任务. 设置提取 首先,我们需要获取一些HT ...
- MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息
MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...
- MVVM模式解析和在WPF中的实现(五)View和ViewModel的通信
MVVM模式解析和在WPF中的实现(五) View和ViewModel的通信 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 M ...
- MVVM模式解析和在WPF中的实现(三)命令绑定
MVVM模式解析和在WPF中的实现(三) 命令绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- Html中的文本框和单选按钮
Html中的文本框和单选按钮用来制作页面的登录注册使用.. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN& ...
- 如何使用免费PDF控件从PDF文档中提取文本和图片
如何使用免费PDF控件从PDF文档中提取文本和图片 概要 现在手头的项目有一个需求是从PDF文档中提取文本和图片,我以前也使用过像iTextSharp, PDFBox 这些免费的PD ...
- s:textarea中的文本内容在什么时候才能被赋值给Action中的属性?
下面是jsp程序片段: <s:form id="startForm" name ="startForm" action="/hall/hall_ ...
随机推荐
- 推荐一款很好用的调试js的eclipse插件
ie调试的话用 Companion.JS 一个插件 很好用的 不用配置,直接安装eclipse调试的话 可以用jsdt 可能需要配置下 网上有很多说明http://wokaours.blog.163. ...
- TibetanFont | ཡིག་གཟུགས། | 藏文字体
1.Microsoft Himalaya 微软喜马拉雅字体 2007年1月30日,微软公司向全球市场同步发布了其最新操作系统Windows Vista,并自带藏文字体和输入法,Windows Vist ...
- CollectionView旋转水平卡片布局
概述 UICollectionView真的好强大,今天我们来研究一下这种很常见的卡片动画效果是如何实现了.本篇不能太深入地讲解,因为笔者也是刚刚摸索出点眉目,但是并没有深刻地理解.如果在讲解过程中,出 ...
- 使用Visual Studio 利用WinGDB编译和远程调试嵌入式Linux的程序
写这篇文章的目的在于帮助那些既要使用Visual Studio编写程序又要开发和调试嵌入式Linux 程序的苦命程序员们! 第一步, 安装 WinGDB ,下载位置 http://www.wingd ...
- Macaca框架
收藏 http://www.cnblogs.com/jinjiangongzuoshi/p/6537795.html
- IJ:IJ来了
ylbtech-IJ:IJ来了 1.返回顶部 1. 2. 2.2. 3. 4. 2.返回顶部 1. 2. 3. 3. Welcome to IntelliJ IDEA返回顶部 1. 2.2. 2.2. ...
- gerrit调试
- Git学习二
一.在工作区和暂存区退回 在工作区: $git checkout -- filename 在暂存区: $git reset HEAD filename $git checkout -- filenam ...
- c语言类型修饰符及内存
今天来学习一下c语言类型修饰符及内存分布 1.auto int a; 默认在内存 2.register int a; 限制变量定义在寄存器上的修饰符 编译器会尽量安排CPU的寄存器去存放这个a,如果寄 ...
- shiro之jdbcRealm
Shiro认证过程 创建SecurityManager--->主体提交认证--->SecurityManager认证--->Authenticsto认证--->Realm验证 ...