WinForm使用正则表达式提取内容的方法示例

2019-12-30 17:44:01刘景俊

本文实例讲述了WinForm使用正则表达式提取内容的方法。,具体如下:

用VS新建WinForm程序,窗体上是三个文本框和一个按钮。

可以自己构造正则表达式,自己修改匹配内容

正则表达是要提取的部分为hewenqitext

代码如下:


using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace HoverTreeBatch.HoverTree
{
public partial class RegexForm : Form
{
public RegexForm()
{
InitializeComponent();
textBox_regex.Text = @"<imgb[^<>]*?bsrc[strn]*=[strn]*[""']?[strn]*(?<hewenqitext>[^strn""'<>]*)[^<>]*?/?[strn]*>";
textBox_content.Text = @"<div class=""dengxaing-hovertree-com""><a title=""批量2"" href=""http://www.easck.com/></a><a title=""VS2015开发人员命令提示查看PublicKeyToken(公钥标记)"" href=""http://www.easck.com/></a><a title=""消灭僵尸游戏"" href=""http://www.easck.com/></a><a title=""纯CSS3漂亮的房子不错的天气"" href=""http://www.easck.com/></a><a title=""HoverClock"" href=""http://www.easck.com/></a><a title=""javascript元旦倒计时特效"" href=""http://www.easck.com/></a><a title=""下雨"" href=""http://www.easck.com/></a><a title=""直接说喜欢你!"" href=""http://www.easck.com/></a><a title=""IIS7,IIS7.5 URL重写模块工具"" href=""http://www.easck.com/></a><a title=""山水画"" href=""http://www.easck.com/></a><a title=""刘德华"" href=""http://www.easck.com/></a><a title=""IIS为网站设置域名"" href=""http://www.easck.com/></a><a title=""HoverTreeCMS分成"" href=""http://www.easck.com/></a><a title=""范冰冰"" href=""http://www.easck.com/></a><a title=""周末愉快"" href=""http://www.easck.com/></a><a title=""谢霆锋手机背景图"" href=""http://www.easck.com/></a><a title=""现代舞舞蹈家玛莎·葛兰姆动画Logo"" href=""http://www.easck.com/></a><a title=""每次吵架,都希望男票这样解决战斗。。。"" href=""http://www.easck.com/></a><a title=""现代舞舞蹈家玛莎·葛兰姆动画Logo"" href=""http://www.easck.com/></a><a title=""美女"" href=""http://www.easck.com/></a></div>";
}
private void button_ok_Click(object sender, EventArgs e)
{
Regex m_hvtRegImg = new Regex(textBox_regex.Text, RegexOptions.IgnoreCase);
//搜索匹配的字符串
MatchCollection hewenqi_matches;
try
{
hewenqi_matches = m_hvtRegImg.Matches(textBox_content.Text);
// 取得匹配项列表
foreach (Match match in hewenqi_matches)
{
textBox_result.Text = textBox_result.Text + match.Groups["hewenqitext"].Value + "rn";
}
}
catch (Exception ex)
{
textBox_result.Text = ex.ToString();
}
}
}
}
/*
<div class="dengxaing-hovertree-com">
<a title="批量2" href=""http://www.easck.com/></a>
<a title="VS2015开发人员命令提示查看PublicKeyToken(公钥标记)" href=""http://www.easck.com/></a>
<a title="消灭僵尸游戏" href=""http://www.easck.com/></a>
<a title="纯CSS3漂亮的房子不错的天气" href=""http://www.easck.com/></a>
<a title="HoverClock" href=""http://www.easck.com/></a>
<a title="javascript元旦倒计时特效" href=""http://www.easck.com/></a>
<a title="下雨" href=""http://www.easck.com/></a>
<a title="直接说喜欢你!" href=""http://www.easck.com/></a>
<a title="IIS7,IIS7.5 URL重写模块工具" href=""http://www.easck.com/></a>
<a title="山水画" href=""http://www.easck.com/></a>
<a title="刘德华" href=""http://www.easck.com/></a>
<a title="IIS为网站设置域名" href=""http://www.easck.com/></a>
<a title="HoverTreeCMS分成" href=""http://www.easck.com/></a>
<a title="范冰冰" href=""http://www.easck.com/></a>
<a title="周末愉快" href=""http://www.easck.com/></a>
<a title="谢霆锋手机背景图" href=""http://www.easck.com/></a>
<a title="现代舞舞蹈家玛莎·葛兰姆动画Logo" href=""http://www.easck.com/></a>
<a title="每次吵架,都希望男票这样解决战斗。。。" href=""http://www.easck.com/></a>
<a title="现代舞舞蹈家玛莎·葛兰姆动画Logo" href=""http://www.easck.com/></a>
<a title="美女" href=""http://www.easck.com/></a>
</div>
*/