【发布时间】:2011-10-08 14:53:44
【问题描述】:
如何从字符串中删除锚标记,我有一个大文本,其中某些单词具有锚标记我想删除该锚标记并希望显示普通单词(没有锚标记)。我的文字是这样的:
LoremIpsum.Net 是一个小而简单的静态站点, provides你有一个大小合适的通道,而不必使用 发电机。该网站还提供了文本的全大写版本,如 还有翻译,还有这个著名的explanation。
【问题讨论】:
如何从字符串中删除锚标记,我有一个大文本,其中某些单词具有锚标记我想删除该锚标记并希望显示普通单词(没有锚标记)。我的文字是这样的:
LoremIpsum.Net 是一个小而简单的静态站点, provides你有一个大小合适的通道,而不必使用 发电机。该网站还提供了文本的全大写版本,如 还有翻译,还有这个著名的explanation。
【问题讨论】:
【讨论】:
如果您想要一个非常简单(且非防弹)的示例,请参见下文。不过,我仍然必须强烈建议您找到一个“合适的”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 是一个小型且简单的静态站点,无需使用生成器即可为您提供大小合适的通道。该网站还提供文本的全大写版本,以及翻译,以及对这个著名内容的解释。
【讨论】:
(.*?) 替换为该文本,然后在 Replace 调用中替换 "$1"再次使用该文本。
这是我剥离 Html 的代码:
public static string StripHTML(this string HTMLText)
{
var reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
return reg.Replace(HTMLText, "").Replace(" ", " ");
}
【讨论】: