【问题标题】:Remove anchor tag from Text从文本中删除锚标记
【发布时间】:2011-10-08 14:53:44
【问题描述】:

如何从字符串中删除锚标记,我有一个大文本,其中某些单词具有锚标记我想删除该锚标记并希望显示普通单词(没有锚标记)。我的文字是这样的:

LoremIpsum.Net 是一个小而简单的静态站点, provides你有一个大小合适的通道,而不必使用 发电机。该网站还提供了文本的全大写版本,如 还有翻译,还有这个著名的explanation

【问题讨论】:

标签: c# asp.net regex


【解决方案1】:

这里有很好的例子:

http://www.dotnetperls.com/remove-html-tags

【讨论】:

    【解决方案2】:

    如果您想要一个非常简单(且非防弹)的示例,请参见下文。不过,我仍然必须强烈建议您找到一个“合适的”html 解析器。

    using System;
    using System.Text.RegularExpressions;
    
    public class Test
    {
            public static void Main()
            {
                    String sample = "<a href=\"http://test.com\" rel=\"nofollow\">LoremIpsum.Net</a> is a small and simple static site that <a href=\"http://test123.com\" rel=\"nofollow\">provides</a> you with a decent sized passage without having to use a generator. The site also provides an all caps version of the text, as well as translations, and an <a href=\"http://test445.com\" rel=\"nofollow\">explanation</a> of what this famous.";
    
                    String re = @"<a [^>]+>(.*?)<\/a>";
                    Console.WriteLine(Regex.Replace(sample, re, "$1"));
            }
    }
    

    输出

    LoremIpsum.Net 是一个小型且简单的静态站点,无需使用生成器即可为您提供大小合适的通道。该网站还提供文本的全大写版本,以及翻译,以及对这个著名内容的解释。

    【讨论】:

    • 如果我只想删除一些选定的锚标记,我需要做什么?例如,如果我想删除“提供”的锚标记。
    • @askiitians:如果您知道锚点中的文本,请将 re 字符串中的 (.*?) 替换为该文本,然后在 Replace 调用中替换 "$1"再次使用该文本。
    【解决方案3】:

    这是我剥离 Html 的代码:

    public static string StripHTML(this string HTMLText)
    {
        var reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
        return reg.Replace(HTMLText, "").Replace("&nbsp;", " ");
    }
    

    【讨论】:

    • 如果我只想删除一些选定的锚标记,我需要做什么?例如,如果我想删除“提供”的锚标记。
    • 那么你应该检查一下 HtmlAgilityPack。
    猜你喜欢
    • 2014-07-02
    • 2013-12-10
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2019-03-13
    • 2010-11-02
    相关资源
    最近更新 更多