【问题标题】:Remove the hidden characters from the string从字符串中删除隐藏的字符
【发布时间】:2018-08-24 18:10:49
【问题描述】:

我正在尝试从下面的字符串中删除标记​​p>

 string name = results[i].ToString();
 var b = Regex.Replace(name, "<.*?>",string.Empty);

字符串name 看起来像&amp;lt;div class="ExternalClassA6E"&amp;gt;&amp;lt;p&amp;gt;​&amp;lt;span&amp;gt;GET6&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;

当我调试时,我在 b 中看不到任何额外的 spl 字符。但是当在应用程序中看到它在变量 b 的前面运行 ? 时,比如 ?GET6。这里的标记有什么特别之处吗?

【问题讨论】:

  • 您已经发布了带有 html 实体的字符串值。该正则表达式永远不会匹配这样的 html 编码字符串。因此,您要么需要在正则表达式中使用实体,要么您的输入看起来不同。另一方面 ? micht 指向字符编码的差异。
  • 是的,?或 � 向我们的用户表明我们错误地处理了他们的数据并对其进行了损坏。你抓住它真是太好了。问题是缺乏沟通或无法控制应该使用哪种字符编码来阅读文本。它必须是用于编写它的字符编码。

标签: c# html regex


【解决方案1】:

我复制了你上面的文字并对其进行了一些测试,它确实表现得很奇怪! 您的字符串中似乎有一个实际隐藏的字符,它不会显示在编辑器中,但会在字符串被解析或写入控制台时出现。

为了测试您所说的内容,我将您的字符串粘贴到我的编辑器中并运行了一些代码,并且我还在输出中看到了 ? 字符。然后我输入了相同的文本并运行了相同的测试,但那里没有?

private static void Main()
{
    string copiedText = "&gt;​&lt;";
    string typedText  = "&gt;&lt;";

    Console.WriteLine("\nCopied Text Results\n" + "-------------------");
    Console.WriteLine("\nLength: " + copiedText.Length);
    Console.WriteLine("\nCharacters and ascii values:");
    Console.WriteLine(string.Join(", ",
        copiedText.Select(character => character + " = " + (int) character)));
    Console.WriteLine("\nString value:");
    Console.WriteLine(copiedText);
    Console.WriteLine("\nHtml Decoded value:");
    Console.WriteLine(HttpUtility.HtmlDecode(copiedText));

    Console.WriteLine(Environment.NewLine + new string('-', Console.WindowWidth));

    Console.WriteLine("\nTyped Text Results\n" + "------------------");
    Console.WriteLine("\nLength: " + typedText.Length);
    Console.WriteLine("\nCharacters and ascii values:");
    Console.WriteLine(string.Join(", ",
        typedText.Select(character => character + " = " + (int) character)));
    Console.WriteLine("\nString value:");
    Console.WriteLine(typedText);
    Console.WriteLine("\nHtml Decoded value:");
    Console.WriteLine(HttpUtility.HtmlDecode(typedText));

    GetKeyFromUser("\nDone! Press any key to exit...");
}

输出

我想这不是一个真正的答案,所以我会尽快删除它,但也许它会激发其他人提供一些反馈。

【讨论】:

    猜你喜欢
    • 2013-02-21
    • 2013-01-17
    • 2020-05-04
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 2016-05-04
    • 2014-08-06
    • 1970-01-01
    相关资源
    最近更新 更多