如何使用正则表达式将HTML中的空行转换为纯文本?

3

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

你所说的“line blank”是什么意思?你的意思是换行符吗? - Ghasem
是的,我想要删除空内容的换行符。 - TRI ÂN
这是实时代码:http://regexr.com/3cmmi - TRI ÂN
不要使用RegEx处理HTML。RegEx是用于正则语言而HTML并不是其中之一。你应该使用HtmlAgilityPack来解析HTML。 - Enigmativity
3个回答

1
正如此处所提到的那样,您可以使用免费且开源的HtmlAgilityPack。请查看示例

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>

And you'll get a plain text result like:

hello world!
it is me!

0
不要在HTML中使用正则表达式。正则表达式是针对常规语言的,而HTML不是。你应该使用HtmlAgilityPack来解析HTML。
这样做非常简单:
var document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(html);

string[] lines =
    document
        .DocumentNode
        .Descendants("li")
        .Select(x => System.Net.WebUtility.HtmlDecode(x.InnerText))
        .ToArray();

string text = String.Join(Environment.NewLine, lines);

我得到的信息如下:

处理器:Intel® Xeon® E5-2403 1.80GHz,10M缓存,6.4GT/s QPI,无Turbo,4C,80W,最大内存1066MHz
内存:8GB(4x2GB)1333MHz,单排LV RDIMMs,最高可达16GB
硬盘:1TB 7.2K RPM NL SAS 3.5英寸热插拔硬盘
存储控制器:H310 RAID控制器,支持RAID 0、1、5、10
文件访问协议:CIFS、NFS、FTP、SMB3.0、SMB Direct(RDMA)
内部驱动器支持:4个3.5英寸热插拔驱动器托架
电源:1个550W电源(冗余)
操作系统:Window Storage 2008 Workgroup R2版
机箱形式:1U机架式系统
保修:3年ProSupport和NBD现场服务

0
如果我理解你的问题,你想要删除尖括号<>之间的任何内容,并且还要删除换行符,那么可以尝试使用以下正则表达式。
@"<[^>]*>|\n"

然而,正如Alex Jolig所建议的那样,请使用HTML Agility Pack。

@"<.*>|\n" also remove text, - TRI ÂN
结果是这些行顶部和底部都是空白的,我想要去掉它们,保留文本。 - TRI ÂN

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接