C#Beginner:在字符串中的两个字符之间删除ALL(正则表达式?)

问题描述 投票:4回答:2

我有一个带有HTML代码的字符串。我想删除所有的HTML标签。所以<和>之间的所有字符。

这是我的代码片段:

WebClient wClient = new WebClient();
SourceCode = wClient.DownloadString( txtSourceURL.Text );
txtSourceCode.Text = SourceCode;
//remove here all between "<" and ">"
txtSourceCodeFormatted.Text = SourceCode;

希望有人能帮助我

c# regex windows
2个回答
13
投票

试试这个:

txtSourceCodeFormatted.Text = Regex.Replace(SourceCode, "<.*?>", string.Empty);

但是,正如其他人提到的那样,handle with care


3
投票

根据Ravi's answer,您可以使用

string noHTML = Regex.Replace(inputHTML, @"<[^>]+>|&nbsp;", "").Trim();

要么

string noHTMLNormalised = Regex.Replace(noHTML, @"\s{2,}", " ");
© www.soinside.com 2019 - 2024. All rights reserved.