c# - 如何使用正则表达式 html 将行空白删除为纯文本?

标签 c# regex nopcommerce

我使用正则表达式将 html 转换为纯文本。

你能帮我用正则表达式删除行空白吗

我的 html:

<div class="short-description">
<div class="short-description">
<div class="short-description">
<div class="short-description">
<div class="short-description">
<div class="short-description">
<div class="short-description">
<div class="short-description">
<div class="short-description">
<ul style="margin: 0px; padding: 0px; font-family: Tahoma, Verdana; color: #000000; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff;">
<li style="margin: 0px; padding: 0px; font-family: Tahoma, Verdana !important;">Processor: Intel® Xeon® E5-2403 1.80GHz, 10M Cache, 6.4GT/s QPI, No Turbo, 4C, 80W, Max Mem 1066MHz</li>
<li style="margin: 0px; padding: 0px; font-family: Tahoma, Verdana !important;">Memory:&nbsp; 8GB (4x2GB) 1333MHz, Single Ranked LV RDIMMs up to 16GB</li>
<li style="margin: 0px; padding: 0px; font-family: Tahoma, Verdana !important;">Hard Drive: 1TB 7.2K RPM NL SAS 3.5-inch Hot Plug</li>
<li style="margin: 0px; padding: 0px; font-family: Tahoma, Verdana !important;">Storage Controller: H310 raid controller Support RAID 0, 1, 5, 10</li>
<li style="margin: 0px; padding: 0px; font-family: Tahoma, Verdana !important;">File Access Protocols: CIFS, NFS, FTP, SMB3.0, SMB Direct (RDMA)</li>
<li style="margin: 0px; padding: 0px; font-family: Tahoma, Verdana !important;">Internal Drive Support: 4 x 3.5" hot-plug drive bays</li>
<li style="margin: 0px; padding: 0px; font-family: Tahoma, Verdana !important;">Power: 1 x 550W Power Supply (redundant)</li>
<li style="margin: 0px; padding: 0px; font-family: Tahoma, Verdana !important;">OS: Window Storage 2008 Workgroup R2 Edition</li>
<li style="margin: 0px; padding: 0px; font-family: Tahoma, Verdana !important;">Form Factor 1U rack mount system</li>
<li style="margin: 0px; padding: 0px; font-family: Tahoma, Verdana !important;">Warranty: 3 Year ProSupport and NBD On-site Service</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
                            </div>

还有我的正则表达式:

Regex.Replace(Model.MetaDescription, @"<(.|\n)*?>","")

这个结果(图片): Result regex.replace

下图怎么样 Result regex.replace

最佳答案

正如它提到的here ,您可以使用免费开源 HtmlAgilityPack .检查sample

a method that converts from HTML to plain text.

var plainText = ConvertToPlainText(string html);

Feed it an HTML string like

<b>hello world!</b><br /><i>it is me! !</i>

你会得到一个纯文本结果,如:

hello world!
it is me!

关于c# - 如何使用正则表达式 html 将行空白删除为纯文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35110358/

相关文章:

c# - 如何在 ASP.NET MVC 中应用长轮询技术?

regex - 如何构建文件名的正则表达式,使其不包含特殊字符且长度有限?

regex - 正则表达式用于识别仅包含特定单词的字符串

c# - Razor HTML Helpers 在 Nop Commerce 插件中给出智能感知错误

c# - 如何从 NopCommerce v3.5 的设置表加载记录

javascript - 如何显示多行的 Highcharts ?

c# - 从 C++ native 插件更新 float 组

c# - 当依赖属性具有 RelativeSource 绑定(bind)时,GetTemplateChild 返回 null

Javascript 正则表达式用包装 <p> 标签替换 textarea 中的换行符

c# - 没有为此对象定义无参数构造函数-nop commerce 2.6