求 去除word格式的正则表达式

jenny0810 2010-01-19 11:42:12
大家好:
我现在有个需求就是关于内容页去除word格式,
现在情况是内容页(包含word格式)存在数据库中,我现在想将word格式去掉如下:
  
<P class=MsoNormal><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一.项目需求说明:</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></B></P>
<P class=a style="MARGIN: 6pt 0cm; TEXT-INDENT: 21pt; LINE-HEIGHT: 150%; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm; mso-char-indent-count: 2.0"><SPAN style="FONT-SIZE: 10.5pt; LINE-HEIGHT: 150%; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 宋体">本软件系统主要应用于市南区政府以及广大的人民群众,方便公众可以随时随地查看政策法规、审批事项等最新服务信息,</SPAN><SPAN style="FONT-SIZE: 10.5pt; COLOR: black; LINE-HEIGHT: 150%; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial">更有效地缩短政府与民众间的距离</SPAN><SPAN style="FONT-SIZE: 10.5pt; LINE-HEIGHT: 150%; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 宋体">。<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></P>
<P class=a style="MARGIN: 6pt 0cm; LINE-HEIGHT: 150%; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm"><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 10.5pt; LINE-HEIGHT: 150%; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 宋体">二.技术需求说明:<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></B></P>
<P class=MsoNormal><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt">2.1</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">综合性能</SPAN></B><SPAN lang=EN-US style="mso-bidi-font-size: 10.5pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 15.75pt; LINE-HEIGHT: 150%; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="LINE-HEIGHT: 150%; mso-bidi-font-size: 10.5pt">1)</SPAN><SPAN style="LINE-HEIGHT: 150%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">要实现</SPAN><SPAN style="COLOR: black; LINE-HEIGHT: 150%; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'verdana,ˎ̥'; mso-hansi-font-family: 'verdana,ˎ̥'; mso-bidi-font-family: Arial">用户随时随地查看最新信息</SPAN><SPAN style="LINE-HEIGHT: 150%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN><SPAN lang=EN-US style="LINE-HEIGHT: 150%; mso-bidi-font-size: 10.5pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 15.75pt; LINE-HEIGHT: 150%; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="LINE-HEIGHT: 150%; mso-bidi-font-size: 10.5pt">2)</SPAN><SPAN style="LINE-HEIGHT: 150%; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">要实现手机和电脑同时访问。</SPAN><SPAN lang=EN-US style="LINE-HEIGHT: 150%; mso-bidi-font-size: 10.5pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 15.75pt; LINE-HEIGHT: 150%; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="LINE-HEIGHT: 150%; mso-bidi-font-size: 10.5pt">3)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">要实现快速查询的功能,只要输入相关主题的关键字就能十分轻松方便地找到您想要的信息。</SPAN></P>
...全文
556 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
sohighthesky 2010-01-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wiki14 的回复:]
好中意楼主的头像啊~

[/Quote]
-过客- 2010-01-19
  • 打赏
  • 举报
回复
try...

Regex reg = new Regex("<[^>]*>");
string result = reg.Replace(yourStr, "");
wiki14 2010-01-19
  • 打赏
  • 举报
回复
好中意楼主的头像啊~
髙興~ 2010-01-19
  • 打赏
  • 举报
回复
不怎么会
自己研究研究吧

http://deerchao.net/tutorials/regex/regex.htm
lovexilove 2010-01-19
  • 打赏
  • 举报
回复
正则一直不会 up
jenny0810 2010-01-19
  • 打赏
  • 举报
回复
替换成如下结果:
   
<br>一.项目需求说明:
<br>本软件系统主要应用于市南区政府以及广大的人民群众,方便公众可以随时随地查看政策法规、审批事项等最新服务信息,更有效地缩短政府与民众间的距离。
<br>二.技术需求说明:
<br>2.1综合性能
<br>1)要实现用户随时随地查看最新信息。
<br>2)要实现手机和电脑同时访问。
<br>3)要实现快速查询的功能,只要输入相关主题的关键字就能十分轻松方便地找到您想要的信息。

如果有table标签则保留table的样式。

谢谢大家
winner2050 2010-01-19
  • 打赏
  • 举报
回复
        /// <summary>
/// 清理Word生成的冗余HTML
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string CleanWordHtml(string html)
{
StringCollection sc = new StringCollection();
// get rid of unnecessary tag spans (comments and title)
sc.Add(@"<!--(\w|\W)+?-->");
sc.Add(@"<title>(\w|\W)+?</title>");
// Get rid of classes and styles
sc.Add(@"\s?class=\w+");
sc.Add(@"\s+style='[^']+'");
// Get rid of unnecessary tags
//sc.Add(@"<(meta|link|/?o:|/?style|/?div|/?st\d|/?head|/?html|body|/?body|/?span|!\[)[^>]*?>");
sc.Add(@"<(meta|link|/?o:|/?style|/?font|/?strong|/?st\d|/?head|/?html|body|/?body|/?span|!\[)[^>]*?>");
// Get rid of empty paragraph tags
sc.Add(@"(<[^>]+>)+ (</\w+>)+");
// remove bizarre v: element attached to <img> tag
sc.Add(@"\s+v:\w+=""[^""]+""");
// remove extra lines
sc.Add(@"(\n\r){2,}");
foreach (string s in sc)
{
html = Regex.Replace(html, s, "", RegexOptions.IgnoreCase);
}
return html;
}
十八道胡同 2010-01-19
  • 打赏
  • 举报
回复

</table>呢?

Regex reg = new Regex("(?i)<(?!/?table\b)[^>]*>");
string result = reg.Replace(yourStr, "");
-过客- 2010-01-19
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jenny0810 的回复:]
我想不替换table标签
[/Quote]

一次把需求说清楚,别再弄出几个标签来。。。
Regex reg = new Regex("(?i)<(?!table\b)[^>]*>");
string result = reg.Replace(yourStr, "");
jenny0810 2010-01-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lxcnn 的回复:]
try...

C# codeRegex reg=new Regex("<[^>]*>");string result= reg.Replace(yourStr,"");
[/Quote]我想不替换table标签
jenny0810 2010-01-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wiki14 的回复:]
一般文本输出就行了。

http://hi.baidu.com/apxsoft/blog/item/3077dc11fdc461c3a7ef3fbf.html

http://hi.baidu.com/apxsoft/blog/item/2e479064db646ff5f73654bf.html

研究研究。
[/Quote]恩 我看看 谢谢
jenny0810 2010-01-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lxcnn 的回复:]
try...

C# codeRegex reg=new Regex("<[^>]*>");string result= reg.Replace(yourStr,"");
[/Quote]这样是不是把table标签也替换掉了呀
谢谢

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧