服务器之家

服务器之家 > 正文

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

时间:2022-01-06 13:09     来源/作者:何问起

本文实例讲述了WinForm使用正则表达式提取内容的方法。分享给大家供大家参考,具体如下:

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

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

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

代码如下:

  1. using System; 
  2. using System.Text.RegularExpressions; 
  3. using System.Windows.Forms; 
  4. namespace HoverTreeBatch.HoverTree 
  5. public partial class RegexForm : Form 
  6. public RegexForm() 
  7. InitializeComponent(); 
  8. 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]*>"
  9. 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>"
  10. private void button_ok_Click(object sender, EventArgs e) 
  11. Regex m_hvtRegImg = new Regex(textBox_regex.Text, RegexOptions.IgnoreCase); 
  12. //搜索匹配的字符串 
  13. MatchCollection hewenqi_matches; 
  14. try 
  15. hewenqi_matches = m_hvtRegImg.Matches(textBox_content.Text); 
  16. // 取得匹配项列表 
  17. foreach (Match match in hewenqi_matches) 
  18. textBox_result.Text = textBox_result.Text + match.Groups["hewenqitext"].Value + "\r\n"
  19. catch (Exception ex) 
  20. textBox_result.Text = ex.ToString(); 
  21. /* 
  22. <div class="dengxaing-hovertree-com"> 
  23. <a title="批量2" href=""http://localhost/hvtimg/201601/p3t2ldyr.png" rel="external nofollow" rel="external nofollow" "> 
  24. <img src=""http://localhost/hvtimg/201601/p3t2ldyr_l.png"" style="width:180px;height:140px;" /></a> 
  25. <a title="VS2015开发人员命令提示查看PublicKeyToken(公钥标记)" href=""http://localhost/hvtimg/201601/wn0bamxp.png" rel="external nofollow" rel="external nofollow" "> 
  26. <img src=""http://localhost/hvtimg/201601/wn0bamxp_l.png"" style="width:180px;height:140px;" /></a> 
  27. <a title="消灭僵尸游戏" href=""http://localhost/hvtimg/201601/q47add2k.jpg" rel="external nofollow" rel="external nofollow" "> 
  28. <img src=""http://localhost/hvtimg/201601/q47add2k_l.jpg"" style="width:180px;height:140px;" /></a> 
  29. <a title="纯CSS3漂亮的房子不错的天气" href=""http://localhost/hvtimg/201601/lbau8hqb.png" rel="external nofollow" rel="external nofollow" "> 
  30. <img src=""http://localhost/hvtimg/201601/lbau8hqb_l.png"" style="width:180px;height:140px;" /></a> 
  31. <a title="HoverClock" href=""http://localhost/hvtimg/201601/thbniyjx.png" rel="external nofollow" rel="external nofollow" "> 
  32. <img src=""http://localhost/hvtimg/201601/thbniyjx_l.png"" style="width:180px;height:140px;" /></a> 
  33. <a title="javascript元旦倒计时特效" href=""http://localhost/hvtimg/201512/o9qashmi.gif" rel="external nofollow" rel="external nofollow" > 
  34. <img src=""http://localhost/hvtimg/201512/o9qashmi_l.gif" style="width:180px;height:140px;" /></a> 
  35. <a title="下雨" href=""http://localhost/hvtimg/201512/f748s0ko.jpg" rel="external nofollow" rel="external nofollow" "> 
  36. <img src=""http://localhost/hvtimg/201512/f748s0ko_l.jpg"" style="width:180px;height:140px;" /></a> 
  37. <a title="直接说喜欢你!" href=""http://localhost/hvtimg/201512/vt1tf0xl.jpg" rel="external nofollow" rel="external nofollow" "> 
  38. <img src=""http://localhost/hvtimg/201512/vt1tf0xl_l.jpg"" style="width:180px;height:140px;" /></a> 
  39. <a title="IIS7,IIS7.5 URL重写模块工具" href=""http://localhost/hvtimg/201512/ew4yb78b.png" rel="external nofollow" rel="external nofollow" "> 
  40. <img src=""http://localhost/hvtimg/201512/ew4yb78b_l.png"" style="width:180px;height:140px;" /></a> 
  41. <a title="山水画" href=""http://localhost/hvtimg/201512/agagq0or.jpg" rel="external nofollow" rel="external nofollow" "> 
  42. <img src=""http://localhost/hvtimg/201512/agagq0or_l.jpg"" style="width:180px;height:140px;" /></a> 
  43. <a title="刘德华" href=""http://localhost/hvtimg/201512/r51a22uy.gif" rel="external nofollow" rel="external nofollow" > 
  44. <img src=""http://localhost/hvtimg/201512/r51a22uy_l.gif" style="width:180px;height:140px;" /></a> 
  45. <a title="IIS为网站设置域名" href=""http://localhost/hvtimg/201512/69yoeuhf.jpg" rel="external nofollow" rel="external nofollow" "> 
  46. <img src=""http://localhost/hvtimg/201512/69yoeuhf_l.jpg"" style="width:180px;height:140px;" /></a> 
  47. <a title="HoverTreeCMS分成" href=""http://localhost/hvtimg/201512/jgi94fdf.png" rel="external nofollow" rel="external nofollow" "> 
  48. <img src=""http://localhost/hvtimg/201512/jgi94fdf_l.png"" style="width:180px;height:140px;" /></a> 
  49. <a title="范冰冰" href=""http://localhost/hvtimg/201511/6j9j6tk5.png" rel="external nofollow" rel="external nofollow" "> 
  50. <img src=""http://localhost/hvtimg/201511/6j9j6tk5_l.png"" style="width:180px;height:140px;" /></a> 
  51. <a title="周末愉快" href=""http://localhost/hvtimg/201511/60mj4wsb.gif" rel="external nofollow" rel="external nofollow" > 
  52. <img src=""http://localhost/hvtimg/201511/60mj4wsb_l.gif" style="width:180px;height:140px;" /></a> 
  53. <a title="谢霆锋手机背景图" href=""http://localhost/hvtimg/201508/cnvkv745.jpg" rel="external nofollow" rel="external nofollow" "> 
  54. <img src=""http://localhost/hvtimg/201508/cnvkv745_l.jpg"" style="width:180px;height:140px;" /></a> 
  55. <a title="现代舞舞蹈家玛莎·葛兰姆动画Logo" href=""http://localhost/hvtimg/201506/ufigfjvn.jpg" rel="external nofollow" rel="external nofollow" "> 
  56. <img src=""http://localhost/hvtimg/201506/ufigfjvn_l.jpg"" style="width:180px;height:140px;" /></a> 
  57. <a title="每次吵架,都希望男票这样解决战斗。。。" href=""http://localhost/hvtimg/201506/iqoqrhfn.gif" rel="external nofollow" rel="external nofollow" > 
  58. <img src=""http://localhost/hvtimg/201506/iqoqrhfn_l.gif" style="width:180px;height:140px;" /></a> 
  59. <a title="现代舞舞蹈家玛莎·葛兰姆动画Logo" href=""http://localhost/hvtimg/201506/xlyxp4wg.png" rel="external nofollow" rel="external nofollow" "> 
  60. <img src=""http://localhost/hvtimg/201506/xlyxp4wg_l.png"" style="width:180px;height:140px;" /></a> 
  61. <a title="美女" href=""http://localhost/hvtimg/201506/m921jw4n.jpg" rel="external nofollow" rel="external nofollow" "> 
  62. <img src=""http://localhost/hvtimg/201506/m921jw4n_l.jpg"" style="width:180px;height:140px;" /></a> 
  63. </div> 
  64. */ 
希望本文所述对大家C#程序设计有所帮助。

 

相关文章

热门资讯

蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部