【发布时间】:2014-01-27 09:04:50
【问题描述】:
这是我使用正则表达式的功能。它工作正常,但它的标签非常缓慢。 我认为它是逐字符搜索html代码。所以它工作得很慢。有没有工作慢的解决办法。
string s = Sourcecode(richTextBox6.Text);
// <a ... > </a> tagları arasını alıyor.(taglar dahil)
Regex regex = new Regex("(?i)<a([^>]+)>(.+?)</a>");
string gelen = s;
string inside = null;
Match match = regex.Match(gelen);
if (match.Success)
{
inside= match.Value;
richTextBox2.Text = inside;
}
string outputStr = "";
foreach (Match ItemMatch in regex.Matches(gelen))
{
Console.WriteLine(ItemMatch);
inside = ItemMatch.Value;
//boşluk bırakıp al satır yazıyor
outputStr += inside + "\r\n";
}
richTextBox2.Text = outputStr;
【问题讨论】:
-
不要使用正则表达式。使用适当的 HTML 解析库,如 Html Agility Pack。您会看到速度提高了十倍。
-
有什么不同的想法为什么它的标签速度很慢?
-
如果有很多要追加的内容,这可能会减慢速度。
outputStr += inside + "\r\n"; -
我喜欢这些问题.... 参考..stackoverflow.com/questions/1732348/… 进行 HTML 正则表达式解析
-
啊啊啊!又一个“用正则表达式解析 HTML”问题!没人再找了吗? “正则表达式 html 解析”在此处的热门搜索发现 Using regular expressions to parse HTML: why not? 作为最佳结果。