【发布时间】:2015-08-12 13:06:31
【问题描述】:
我正在尝试解析一个字符串并从中删除“表情符号”并保留新行。
所以,我有这段代码:
string text = "S H A V A . Est 2015\nBandung\nLine: @ubm5921j\nBbm: 7D2E6310\nFAST ORDER\ud83d\udc47\ud83c\udffe\ud83d\udc47\ud83c\udffe";
MessageBox.Show(text);
string result = Regex.Replace(text, @"\p{Cs}", "");
这里'text'的输出如下:
因此,您可以看到新行运行良好,并且它的末尾有“表情符号”,而下一行则完美地删除了它们。所以结果字符串将包含相同的字符串,但没有换行符。
在程序的另一部分我有这段代码。
//uu.description is the same string as above 'text',
//this is where I scrape directly from html
string text2 = uu.description;
MessageBox.Show(text2);
string result2 = Regex.Replace(text2, @"\p{Cs}", "");
正如您在本例中看到的那样,我的 text2 以原样输出格式的字符串,而正则表达式完全没有任何作用。新行不起作用,表情符号也不会被删除。
我很困惑为什么它在我的第一种情况下有效,而在第二种情况下无效。我已经研究了好几个小时了,还是搞不明白。
【问题讨论】:
-
尝试
string text2 = Regex.Unescape(uu.description);并将字符替换为@"\p{Cs}"。或者检查抓取代码:你会在某个时候得到所有字符的转义。请显示 HTML 抓取代码。 -
string text2 = HttpUtility.HtmlDecode(uu.description); -
string text2 = WebUtility.HtmlDecode(uu.description);如果您使用 .NET 4.0 及更高版本 -
@stribizhev 抓取代码有点长,无法显示。基本上,我有一个 Web 客户端,我使用 requestString() 下载页面,然后将其从那里刮掉。我试过
string lmao = Regex.Unescape(uu.description);然后lmao = Regex.Replace(testz, @"\p{Cs}", "");但结果相同 -
你已经获得了学分,顺其自然。下次请告知建议的解决方案是否适合您,并感谢获得该解决方案的人。