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

 更新时间:2020年6月25日 11:19  点击:1765

本文实例讲述了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正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript

正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg

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

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

[!--infotagslink--]

相关文章

  • PHP正则表达式取双引号内的内容

    取双引号内的内容我们如果一个字符串中只有一个可以使用explode来获得,但如果有多个需要使用正则表达式来提取了,具体的例子如下。 写程序的时候总结一点经验,如何只...2016-11-25
  • 深入分析C#中WinForm控件之Dock顺序调整的详解

    本篇文章是对C#中WinForm控件之Dock顺序调整进行了详细的分析介绍,需要的朋友参考下...2020-06-25
  • PHP正则表达式之捕获组与非捕获组

    今天遇到一个正则匹配的问题,忽然翻到有捕获组的概念,手册上也是一略而过,百度时无意翻到C#和Java中有对正则捕获组的特殊用法,搜索关键词有PHP时竟然没有相关内容,自己试了一下,发现在PHP中也是可行的,于是总结一下,分享的同...2015-11-08
  • php 验证只能输入汉字、英语、数字的正则表达式

    正则表达式是一门非常有用的并且进行模糊判断的一个功能了,我们下面来看通过正则来验证输入汉字、英语、数字,具体如下。 收藏了正则表达式。可以验证只能输入数...2016-11-25
  • java正则表达式判断前端参数修改表中另一个字段的值

    这篇文章主要介绍了java正则表达式判断前端参数修改表中另一个字段的值,需要的朋友可以参考下...2021-05-07
  • C# Winform中实现主窗口打开登录窗口关闭的方法

    这篇文章主要介绍了C# Winform中实现主窗口打开登录窗口关闭的方法,这在需要用户名密码的软件项目中是必用的一个技巧,要的朋友可以参考下...2020-06-25
  • C# winform打开Excel文档的方法总结(必看篇)

    下面小编就为大家带来一篇C# winform打开Excel文档的方法总结(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • 常用的日期时间正则表达式

    常用的日期时间正则表达式 下面收藏了大量的日期时间正则匹配函数,包括分钟,时间与秒都能达到。 正则表达式 (?n:^(?=d)((?<day>31(?!(.0?[2469]|11))|30(?!.0?2)|29(...2016-11-25
  • PHP正则表达式匹配验证提取网址URL实例总结

    网址规则是可寻的,所以我们可以使用正则表达式来提取字符串中的url地址了,下面一起来看看小编整理的几个PHP正则表达式匹配验证提取网址URL实例. 匹配网址 URL 的...2016-11-25
  • C# WinForm快捷键设置技巧

    这篇文章主要给大家介绍C# winform快捷键设置技巧,涉及到C winform快捷键相关知识,对C winform知识感兴趣的朋友可以参考下本篇文章...2020-06-25
  • c#中Winform实现多线程异步更新UI(进度及状态信息)

    本篇文章主要介绍了c#中Winform实现多线程异步更新UI(进度及状态信息) ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • 正则表达式中两个反斜杠的匹配规则详解

    这篇文章主要介绍了正则表达式中两个反斜杠的匹配规则,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-05-07
  • JS中使用正则表达式g模式和非g模式的区别

    这篇文章给大家详细介绍了JS中使用正则表达式g模式和非g模式的区别,非常不错,具有参考借鉴价值,需要的朋友参考下吧...2017-04-03
  • C#正则表达式使用方法示例

    这篇文章主要介绍了C#正则表达式使用方法,大家参考使用...2020-06-25
  • c# winform异步不卡界面的实现方法

    这篇文章主要给大家介绍了关于c# winform异步不卡界面的实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-25
  • C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法

    这篇文章主要介绍了C# WinForm程序处理后台繁忙导致前台控件假死现象解决方法,本文通过Application.DoEvents()解决这个问题,并讲解了Application.DoEvents()的作用,需要的朋友可以参考下...2020-06-25
  • C#中winform使用相对路径读取文件的方法

    这篇文章主要介绍了C#中winform使用相对路径读取文件的方法,实例分析了C#使用相对路径读取文件的技巧与实际应用,需要的朋友可以参考下...2020-06-25
  • 常用C#正则表达式汇总介绍

    c#正则表达式,用于字符串处理、表单验证等场合,实用高效。现将一些常用的表达式收集于此,以备不时之需。...2020-06-25
  • JavaScript利用正则表达式替换字符串中的内容

    本文主要介绍了JavaScript利用正则表达式替换字符串中内容的具体实现方法,并做了简要注释,便于理解。具有一定的参考价值,需要的朋友可以看下...2017-01-09
  • 一文秒懂python正则表达式常用函数

    这篇文章主要介绍了python正则表达式常用函数及使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-05-07