我使用正则表达式将 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: 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/