Match.Result()、Match.Groups[] 正则.NET常用

来源:互联网 发布:关联规则的经典算法 编辑:程序博客网 时间:2024/05/16 04:42
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
?
0 0
原创粉丝点击