【问题标题】:How to remove line blank with regex html to plain text?如何使用正则表达式 html 将空白行删除为纯文本?
【发布时间】:2016-05-08 17:07:14
【问题描述】:

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

【问题讨论】:

  • 行空白是什么意思?你的意思是换行符吗?
  • 是的,我想删除空内容的换行符
  • 这是实时代码:regexr.com/3cmmi
  • 不要在 HTML 中使用 RegEx。 RegEx 适用于常规语言,而 HTML 不是其中之一。您应该使用 HtmlAgilityPack 来解析 HTML。

标签: c# regex nopcommerce


【解决方案1】:

正如上面提到的here,您可以使用免费和开源的HtmlAgilityPack。检查sample

从 HTML 转换为纯文本的方法。

var plainText = ConvertToPlainText(string html);

给它一个 HTML 字符串,比如

世界你好!
是我! !

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

hello world!
it is me!

【讨论】:

    【解决方案2】:

    如果我理解这个问题,您想删除尖括号 &lt;&gt; 之间的任何内容并删除换行符,然后试试这个正则表达式

    @"<[^>]*>|\n"
    

    不过,正如 Alex Jolig 所建议的,请使用 HTML Agility Pack。

    【讨论】:

    • @"<.>|\n" 也删除文本,
    • 结果这些行在顶部和底部都是空白的,我想删除它,并保留文本。
    【解决方案3】:

    不要在 HTML 中使用 RegEx。 RegEx 适用于常规语言,而 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,Max Mem 1066MHz 内存:8GB (4x2GB) 1333MHz,单列 LV RDIMM,最高 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 x 3.5" 热插拔驱动器托架 电源:1 x 550W 电源(冗余) 操作系统:Window Storage 2008 Workgroup R2 版 外形尺寸 1U 机架安装系统 保修:3 年 ProSupport 和 NBD 现场服务

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      相关资源
      最近更新 更多