本文主要是介绍Match.Result()、Match.Groups[] 正则.NET常用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
string html = "<div><span>aaaa</span><a href='http://yuxnet.blog.163.com/blog/'>鏈接</a><img><input type='text' /></div>";
string pattern = "<(?<tag>[a-zA-Z]+)[^<>]*>";
System.Text.RegularExpressions.MatchCollection mc = System.Text.RegularExpressions.Regex.Matches(html, pattern);
foreach (System.Text.RegularExpressions.Match item in mc)
{
this.TextBox1.Text += item.Value;
this.TextBox1.Text += "\n";
this.TextBox2.Text += item.Result("$1");
this.TextBox2.Text += "\n";
this.TextBox3.Text += item.Groups["tag"].Value;
this.TextBox3.Text += "\n";
}?
执行上面的代码,TextBox1结果为:
<div>
<span>
<a href='http://yuxnet.blog.163.com/blog/'>
<img>
<input type='text' />
?
TextBox2结果为:
div
span
a
img
input
?
TextBox3结果为:
div
span
a
img
input
?
string pattern = "<(?<tag>[a-zA-Z]+)[^<>]*>";
System.Text.RegularExpressions.MatchCollection mc = System.Text.RegularExpressions.Regex.Matches(html, pattern);
foreach (System.Text.RegularExpressions.Match item in mc)
{
this.TextBox1.Text += item.Value;
this.TextBox1.Text += "\n";
this.TextBox2.Text += item.Result("$1");
this.TextBox2.Text += "\n";
this.TextBox3.Text += item.Groups["tag"].Value;
this.TextBox3.Text += "\n";
}?
执行上面的代码,TextBox1结果为:
<div>
<span>
<a href='http://yuxnet.blog.163.com/blog/'>
<img>
<input type='text' />
?
TextBox2结果为:
div
span
a
img
input
?
TextBox3结果为:
div
span
a
img
input
?
这篇关于Match.Result()、Match.Groups[] 正则.NET常用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!