Regex reg = new Regex(@"<.*?>"); String str = @"test<my content><your content>"; foreach (Match m in reg.Matches(str)) Console.WriteLine(m.Value);/try..
Regex reg = new Regex(@"(?<=\<)[^<>]+(?=\>)"); MatchCollection mc = reg.Matches(str); foreach (Match m in mc) { TextBox1.Text += m.Value + "\n"; }
我还有个问题,如何过滤网页中的js 注释 和HTML 注释;
js 注释 (?m)([ \t]*/\*((?!\*/)[\s\S])+\*/|((?!//).)*//.*) HTML 注释 <!--[\s\S]*?-->
测试范例 private static void TestRegex05() { string html = @"<script type=""text/javascript""> // 这行代码输出标题: document.write(""<h1>This is a header</h1>""); // 这行代码输出段落: document.write(""<p>This is a paragraph</p>""); document.write(""<p>This is another paragraph</p>""); </script><script type=""text/javascript""> /* 下面的代码将输出 一个标题和两个段落 */ document.write(""<h1>This is a header</h1>""); document.write(""<p>This is a paragraph</p>""); document.write(""<p>This is another paragraph</p>""); </script>"; MatchCollection mc = Regex.Matches(html, @"(?m)([ \t]*/\*((?!\*/)[\s\S])+\*/|((?!//).)*//.*)"); foreach (Match m in mc) { Console.WriteLine(m.Value); } } private static void TestRegex04() { string html = @"<!--到梦之都XHTML教程的链接--> <a href=""http://www.dreamdu.com/xhtml/""> 学习XHTML </a>吧! <!-- 链接结束 -->"; MatchCollection mc = Regex.Matches(html, @"<!--[\s\S]*?-->"); foreach (Match m in mc) { Console.WriteLine(m.Value); } }
foreach (Match m in reg.Matches(str))
Console.WriteLine(m.Value);/try..
MatchCollection mc = reg.Matches(str);
foreach (Match m in mc)
{
TextBox1.Text += m.Value + "\n";
}
我还有个问题,如何过滤网页中的js 注释 和HTML 注释;
(?m)([ \t]*/\*((?!\*/)[\s\S])+\*/|((?!//).)*//.*)
HTML 注释
<!--[\s\S]*?-->
{
string html = @"<script type=""text/javascript"">
// 这行代码输出标题:
document.write(""<h1>This is a header</h1>"");
// 这行代码输出段落:
document.write(""<p>This is a paragraph</p>"");
document.write(""<p>This is another paragraph</p>"");
</script><script type=""text/javascript"">
/*
下面的代码将输出
一个标题和两个段落
*/
document.write(""<h1>This is a header</h1>"");
document.write(""<p>This is a paragraph</p>"");
document.write(""<p>This is another paragraph</p>"");
</script>";
MatchCollection mc = Regex.Matches(html, @"(?m)([ \t]*/\*((?!\*/)[\s\S])+\*/|((?!//).)*//.*)");
foreach (Match m in mc)
{
Console.WriteLine(m.Value);
}
} private static void TestRegex04()
{
string html = @"<!--到梦之都XHTML教程的链接-->
<a href=""http://www.dreamdu.com/xhtml/"">
学习XHTML
</a>吧!
<!--
链接结束
-->";
MatchCollection mc = Regex.Matches(html, @"<!--[\s\S]*?-->");
foreach (Match m in mc)
{
Console.WriteLine(m.Value);
}
}