欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-06 11:50:28
本文实例讲述了winform使用正则表达式提取内容的方法。分享给大家供大家参考,具体如下: 用vs新建winform程序,窗体上是三个文本框和一个按钮。 可以自己构造正...

本文实例讲述了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 = @"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<hewenqitext>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>";
textbox_content.text = @"<div class=""dengxaing-hovertree-com""><a title=""批量2"" href=""http://localhost/hvtimg/201601/p3t2ldyr.png" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201601/p3t2ldyr_l.png"" style=""width:180px;height:140px;"" /></a><a title=""vs2015开发人员命令提示查看publickeytoken(公钥标记)"" href=""http://localhost/hvtimg/201601/wn0bamxp.png" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201601/wn0bamxp_l.png"" style=""width:180px;height:140px;"" /></a><a title=""消灭僵尸游戏"" href=""http://localhost/hvtimg/201601/q47add2k.jpg" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201601/q47add2k_l.jpg"" style=""width:180px;height:140px;"" /></a><a title=""纯css3漂亮的房子不错的天气"" href=""http://localhost/hvtimg/201601/lbau8hqb.png" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201601/lbau8hqb_l.png"" style=""width:180px;height:140px;"" /></a><a title=""hoverclock"" href=""http://localhost/hvtimg/201601/thbniyjx.png" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201601/thbniyjx_l.png"" style=""width:180px;height:140px;"" /></a><a title=""javascript元旦倒计时特效"" href=""http://localhost/hvtimg/201512/o9qashmi.gif" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201512/o9qashmi_l.gif"" style=""width:180px;height:140px;"" /></a><a title=""下雨"" href=""http://localhost/hvtimg/201512/f748s0ko.jpg" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201512/f748s0ko_l.jpg"" style=""width:180px;height:140px;"" /></a><a title=""直接说喜欢你!"" href=""http://localhost/hvtimg/201512/vt1tf0xl.jpg" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201512/vt1tf0xl_l.jpg"" style=""width:180px;height:140px;"" /></a><a title=""iis7,iis7.5 url重写模块工具"" href=""http://localhost/hvtimg/201512/ew4yb78b.png" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201512/ew4yb78b_l.png"" style=""width:180px;height:140px;"" /></a><a title=""山水画"" href=""http://localhost/hvtimg/201512/agagq0or.jpg" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201512/agagq0or_l.jpg"" style=""width:180px;height:140px;"" /></a><a title=""刘德华"" href=""http://localhost/hvtimg/201512/r51a22uy.gif" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201512/r51a22uy_l.gif"" style=""width:180px;height:140px;"" /></a><a title=""iis为网站设置域名"" href=""http://localhost/hvtimg/201512/69yoeuhf.jpg" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201512/69yoeuhf_l.jpg"" style=""width:180px;height:140px;"" /></a><a title=""hovertreecms分成"" href=""http://localhost/hvtimg/201512/jgi94fdf.png" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201512/jgi94fdf_l.png"" style=""width:180px;height:140px;"" /></a><a title=""范冰冰"" href=""http://localhost/hvtimg/201511/6j9j6tk5.png" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201511/6j9j6tk5_l.png"" style=""width:180px;height:140px;"" /></a><a title=""周末愉快"" href=""http://localhost/hvtimg/201511/60mj4wsb.gif" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201511/60mj4wsb_l.gif"" style=""width:180px;height:140px;"" /></a><a title=""谢霆锋手机背景图"" href=""http://localhost/hvtimg/201508/cnvkv745.jpg" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201508/cnvkv745_l.jpg"" style=""width:180px;height:140px;"" /></a><a title=""现代舞舞蹈家玛莎·葛兰姆动画logo"" href=""http://localhost/hvtimg/201506/ufigfjvn.jpg" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201506/ufigfjvn_l.jpg"" style=""width:180px;height:140px;"" /></a><a title=""每次吵架,都希望男票这样解决战斗。。。"" href=""http://localhost/hvtimg/201506/iqoqrhfn.gif" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201506/iqoqrhfn_l.gif"" style=""width:180px;height:140px;"" /></a><a title=""现代舞舞蹈家玛莎·葛兰姆动画logo"" href=""http://localhost/hvtimg/201506/xlyxp4wg.png" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201506/xlyxp4wg_l.png"" style=""width:180px;height:140px;"" /></a><a title=""美女"" href=""http://localhost/hvtimg/201506/m921jw4n.jpg" rel="external nofollow" rel="external nofollow" "><img src=""http://localhost/hvtimg/201506/m921jw4n_l.jpg"" style=""width:180px;height:140px;"" /></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 + "\r\n";
}
}
catch (exception ex)
{
textbox_result.text = ex.tostring();
}
}
}
}
/*
<div class="dengxaing-hovertree-com">
<a title="批量2" href=""http://localhost/hvtimg/201601/p3t2ldyr.png" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201601/p3t2ldyr_l.png"" style="width:180px;height:140px;" /></a>
<a title="vs2015开发人员命令提示查看publickeytoken(公钥标记)" href=""http://localhost/hvtimg/201601/wn0bamxp.png" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201601/wn0bamxp_l.png"" style="width:180px;height:140px;" /></a>
<a title="消灭僵尸游戏" href=""http://localhost/hvtimg/201601/q47add2k.jpg" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201601/q47add2k_l.jpg"" style="width:180px;height:140px;" /></a>
<a title="纯css3漂亮的房子不错的天气" href=""http://localhost/hvtimg/201601/lbau8hqb.png" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201601/lbau8hqb_l.png"" style="width:180px;height:140px;" /></a>
<a title="hoverclock" href=""http://localhost/hvtimg/201601/thbniyjx.png" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201601/thbniyjx_l.png"" style="width:180px;height:140px;" /></a>
<a title="javascript元旦倒计时特效" href=""http://localhost/hvtimg/201512/o9qashmi.gif" rel="external nofollow" rel="external nofollow" >
<img src=""http://localhost/hvtimg/201512/o9qashmi_l.gif" style="width:180px;height:140px;" /></a>
<a title="下雨" href=""http://localhost/hvtimg/201512/f748s0ko.jpg" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201512/f748s0ko_l.jpg"" style="width:180px;height:140px;" /></a>
<a title="直接说喜欢你!" href=""http://localhost/hvtimg/201512/vt1tf0xl.jpg" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201512/vt1tf0xl_l.jpg"" style="width:180px;height:140px;" /></a>
<a title="iis7,iis7.5 url重写模块工具" href=""http://localhost/hvtimg/201512/ew4yb78b.png" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201512/ew4yb78b_l.png"" style="width:180px;height:140px;" /></a>
<a title="山水画" href=""http://localhost/hvtimg/201512/agagq0or.jpg" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201512/agagq0or_l.jpg"" style="width:180px;height:140px;" /></a>
<a title="刘德华" href=""http://localhost/hvtimg/201512/r51a22uy.gif" rel="external nofollow" rel="external nofollow" >
<img src=""http://localhost/hvtimg/201512/r51a22uy_l.gif" style="width:180px;height:140px;" /></a>
<a title="iis为网站设置域名" href=""http://localhost/hvtimg/201512/69yoeuhf.jpg" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201512/69yoeuhf_l.jpg"" style="width:180px;height:140px;" /></a>
<a title="hovertreecms分成" href=""http://localhost/hvtimg/201512/jgi94fdf.png" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201512/jgi94fdf_l.png"" style="width:180px;height:140px;" /></a>
<a title="范冰冰" href=""http://localhost/hvtimg/201511/6j9j6tk5.png" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201511/6j9j6tk5_l.png"" style="width:180px;height:140px;" /></a>
<a title="周末愉快" href=""http://localhost/hvtimg/201511/60mj4wsb.gif" rel="external nofollow" rel="external nofollow" >
<img src=""http://localhost/hvtimg/201511/60mj4wsb_l.gif" style="width:180px;height:140px;" /></a>
<a title="谢霆锋手机背景图" href=""http://localhost/hvtimg/201508/cnvkv745.jpg" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201508/cnvkv745_l.jpg"" style="width:180px;height:140px;" /></a>
<a title="现代舞舞蹈家玛莎·葛兰姆动画logo" href=""http://localhost/hvtimg/201506/ufigfjvn.jpg" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201506/ufigfjvn_l.jpg"" style="width:180px;height:140px;" /></a>
<a title="每次吵架,都希望男票这样解决战斗。。。" href=""http://localhost/hvtimg/201506/iqoqrhfn.gif" rel="external nofollow" rel="external nofollow" >
<img src=""http://localhost/hvtimg/201506/iqoqrhfn_l.gif" style="width:180px;height:140px;" /></a>
<a title="现代舞舞蹈家玛莎·葛兰姆动画logo" href=""http://localhost/hvtimg/201506/xlyxp4wg.png" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201506/xlyxp4wg_l.png"" style="width:180px;height:140px;" /></a>
<a title="美女" href=""http://localhost/hvtimg/201506/m921jw4n.jpg" rel="external nofollow" rel="external nofollow" ">
<img src=""http://localhost/hvtimg/201506/m921jw4n_l.jpg"" style="width:180px;height:140px;" /></a>
</div>
*/

ps:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

javascript正则表达式在线测试工具:

正则表达式在线生成工具:

更多关于c#相关内容感兴趣的读者可查看本站专题:《winform控件用法总结》、《c#窗体操作技巧汇总》、《c#数据结构与算法教程》、《c#常见控件用法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。