WinForm使用正則表達(dá)式提取內(nèi)容的方法示例
本文實例講述了WinForm使用正則表達(dá)式提取內(nèi)容的方法。分享給大家供大家參考,具體如下:
用VS新建WinForm程序,窗體上是三個文本框和一個按鈕。
可以自己構(gòu)造正則表達(dá)式,自己修改匹配內(nèi)容
正則表達(dá)是要提取的部分為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開發(fā)人員命令提示查看PublicKeyToken(公鑰標(biāo)記)"" 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為網(wǎng)站設(shè)置域名"" 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=""現(xiàn)代舞舞蹈家瑪莎·葛蘭姆動畫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=""每次吵架,都希望男票這樣解決戰(zhàn)斗。。。"" 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=""現(xiàn)代舞舞蹈家瑪莎·葛蘭姆動畫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開發(fā)人員命令提示查看PublicKeyToken(公鑰標(biāo)記)" 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為網(wǎng)站設(shè)置域名" 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="現(xiàn)代舞舞蹈家瑪莎·葛蘭姆動畫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="每次吵架,都希望男票這樣解決戰(zhàn)斗。。。" 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="現(xiàn)代舞舞蹈家瑪莎·葛蘭姆動畫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款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《WinForm控件用法總結(jié)》、《C#窗體操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#常見控件用法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》及《C#程序設(shè)計之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計有所幫助。
- 使用C# Winform應(yīng)用程序獲取網(wǎng)頁源文件的解決方法
- C#基于正則表達(dá)式實現(xiàn)獲取網(wǎng)頁中所有信息的網(wǎng)頁抓取類實例
- 使用C#正則表達(dá)式獲取必應(yīng)每日圖片地址
- C#正則表達(dá)式獲取下拉菜單(select)的相關(guān)屬性值
- C#使用正則表達(dá)式抓取網(wǎng)站信息示例
- C#通過正則表達(dá)式實現(xiàn)提取網(wǎng)頁中的圖片
- c# 正則表達(dá)式對網(wǎng)頁進(jìn)行有效內(nèi)容抽取
- 常用正則 常用的C#正則表達(dá)式
- C#的正則表達(dá)式Regex類使用簡明教程
- C# 正則表達(dá)式經(jīng)典分類整理集合手冊
- C#中的正則表達(dá)式 學(xué)習(xí)資料
相關(guān)文章
C#設(shè)計模式之Visitor訪問者模式解決長隆歡樂世界問題實例
這篇文章主要介紹了C#設(shè)計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結(jié)合具體實例形式分析了C#使用訪問者模式解決長隆歡樂世界問題的具體實現(xiàn)技巧,需要的朋友可以參考下2017-09-09C#去掉字符串中所有匹配的字符String.Replace方法
在C#中,如果你想要去掉字符串中所有匹配的字符,你可以使用String.Replace方法,本文主要介紹了C#去掉字符串中所有匹配的字符String.Replace方法,具有一定的參考價值,感興趣的可以了解一下2024-04-04c#用Treeview實現(xiàn)FolderBrowerDialog 和動態(tài)獲取系統(tǒng)圖標(biāo)(運用了Win32 
其實,FolderBrowerDialog 很好用呢,有木有啊親,反正我特別的喜歡,微軟大哥把這個瀏覽文件夾的東東封裝的多好呀2013-03-03C#中數(shù)組初始化與數(shù)組元素復(fù)制的方法
這篇文章主要介紹了C#中數(shù)組初始化與數(shù)組元素復(fù)制的方法,涉及C#中數(shù)組的創(chuàng)建、初始化及使用Array.Copy方法復(fù)制數(shù)組元素的技巧,需要的朋友可以參考下2015-04-04C#網(wǎng)絡(luò)爬蟲代碼分享 C#簡單的爬取工具
這篇文章主要為大家詳細(xì)介紹了C#網(wǎng)絡(luò)爬蟲代碼,教大家如何制作了簡單的爬取工具,感興趣的小伙伴們可以參考一下2016-07-07