C# 我怎样才能得到一个正则表达式,它以类似class=“sampleClass”的html代码给出类名?
我想获取html文件中使用的所有类名。 到目前为止,我已经试过C了C# 我怎样才能得到一个正则表达式,它以类似class=“sampleClass”的html代码给出类名?,c#,regex,C#,Regex,我想获取html文件中使用的所有类名。 到目前为止,我已经试过C了 String data = Uri.UnescapeDataString(TextBox1.Text); List<string> allClass = new List<string>(); Match match = Regex.Match(data, "class=\"[^#]+\""); if (match.Success) { Co
String data = Uri.UnescapeDataString(TextBox1.Text);
List<string> allClass = new List<string>();
Match match = Regex.Match(data, "class=\"[^#]+\"");
if (match.Success)
{
Console.WriteLine(match.Captures[0].Value); // Will output "#item3#"
}
但这并没有达到预期的效果。
像
我的代码是
<div class="dialogBodyWrapper">
<div class="dialogBoxContentParent">
<p class="mediumText">Changing your authentication details will log you out from the current session
and requires re-login with new credentials. Would You like to proceed?</p>
</div>
<div class="clear"></div>
</div>
我想要列表中的类名,比如dialogBodyWrapper、dialogBoxContentParent、mediumText和clear
我尝试了很多正则表达式,但都不适合我。
请帮帮我。省去你的巨大痛苦,从一开始就使用Html敏捷包
为自己省去大量的痛苦,从一开始就使用Html敏捷包
我强烈同意您应该从任何费力的地方使用HtmlAgilityPack—但是—如果这是一个一次性脚本,您可以使用以下内容:
var classes = Regex
.Matches(html, @"class=""(.*?)""")
.Cast<Match>()
.Select(m => m.Groups[1].Value);
我强烈同意您应该从任何费力的地方使用HtmlAgilityPack—但是—如果这是一个一次性脚本,您可以使用以下内容:
var classes = Regex
.Matches(html, @"class=""(.*?)""")
.Cast<Match>()
.Select(m => m.Groups[1].Value);
你的部分问题是你使用了match而不是matches。另外,我会使用一个正则表达式,比如class=[^]+等,当您稍后再讨论它时,它很容易理解。您的部分问题是使用了match而不是matches。另外,我会使用一个正则表达式,比如class=[^]+等,当您稍后回到它时,它很容易理解。no@Traubenfuchs>如果您阅读该线程,您将看到这仍然在讨论中。在这种情况下,我不明白为什么OP不能使用正则表达式。。。但是我可能错了。不@Traubenfuchs>如果你读了这篇文章,你会发现这仍然在讨论中。在这种情况下,我不明白为什么OP不能使用正则表达式。。。但我可能错了。