本文实例讲述了WinForm使用正则表达式提取内容的方法。分享给大家供大家参考,具体如下:
用VS新建WinForm程序,窗体上是三个文本框和一个按钮。
可以自己构造正则表达式,自己修改匹配内容
正则表达是要提取的部分为hewenqitext
代码如下:
usingSystem; usingSystem.Text.RegularExpressions; usingSystem.Windows.Forms; namespaceHoverTreeBatch.HoverTree { publicpartialclassRegexForm:Form { publicRegexForm() { InitializeComponent(); textBox_regex.Text=@"<imgb[^<>]*?bsrc[strn]*=[strn]*[""']?[strn]*(?<hewenqitext>[^strn""'<>]*)[^<>]*?/?[strn]*>"; textBox_content.Text=@"<divclass=""dengxaing-hovertree-com""><atitle=""批量2""href=""https://localhost/hvtimg/201601/p3t2ldyr.png"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201601/p3t2ldyr_l.png""style=""width:180px;height:140px;""/></a><atitle=""VS2015开发人员命令提示查看PublicKeyToken(公钥标记)""href=""https://localhost/hvtimg/201601/wn0bamxp.png"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201601/wn0bamxp_l.png""style=""width:180px;height:140px;""/></a><atitle=""消灭僵尸游戏""href=""https://localhost/hvtimg/201601/q47add2k.jpg"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201601/q47add2k_l.jpg""style=""width:180px;height:140px;""/></a><atitle=""纯CSS3漂亮的房子不错的天气""href=""https://localhost/hvtimg/201601/lbau8hqb.png"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201601/lbau8hqb_l.png""style=""width:180px;height:140px;""/></a><atitle=""HoverClock""href=""https://localhost/hvtimg/201601/thbniyjx.png"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201601/thbniyjx_l.png""style=""width:180px;height:140px;""/></a><atitle=""javascript元旦倒计时特效""href=""https://localhost/hvtimg/201512/o9qashmi.gif"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201512/o9qashmi_l.gif""style=""width:180px;height:140px;""/></a><atitle=""下雨""href=""https://localhost/hvtimg/201512/f748s0ko.jpg"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201512/f748s0ko_l.jpg""style=""width:180px;height:140px;""/></a><atitle=""直接说喜欢你!""href=""https://localhost/hvtimg/201512/vt1tf0xl.jpg"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201512/vt1tf0xl_l.jpg""style=""width:180px;height:140px;""/></a><atitle=""IIS7,IIS7.5URL重写模块工具""href=""https://localhost/hvtimg/201512/ew4yb78b.png"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201512/ew4yb78b_l.png""style=""width:180px;height:140px;""/></a><atitle=""山水画""href=""https://localhost/hvtimg/201512/agagq0or.jpg"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201512/agagq0or_l.jpg""style=""width:180px;height:140px;""/></a><atitle=""刘德华""href=""https://localhost/hvtimg/201512/r51a22uy.gif"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201512/r51a22uy_l.gif""style=""width:180px;height:140px;""/></a><atitle=""IIS为网站设置域名""href=""https://localhost/hvtimg/201512/69yoeuhf.jpg"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201512/69yoeuhf_l.jpg""style=""width:180px;height:140px;""/></a><atitle=""HoverTreeCMS分成""href=""https://localhost/hvtimg/201512/jgi94fdf.png"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201512/jgi94fdf_l.png""style=""width:180px;height:140px;""/></a><atitle=""范冰冰""href=""https://localhost/hvtimg/201511/6j9j6tk5.png"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201511/6j9j6tk5_l.png""style=""width:180px;height:140px;""/></a><atitle=""周末愉快""href=""https://localhost/hvtimg/201511/60mj4wsb.gif"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201511/60mj4wsb_l.gif""style=""width:180px;height:140px;""/></a><atitle=""谢霆锋手机背景图""href=""https://localhost/hvtimg/201508/cnvkv745.jpg"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201508/cnvkv745_l.jpg""style=""width:180px;height:140px;""/></a><atitle=""现代舞舞 家玛莎·葛兰姆动画Logo""href=""https://localhost/hvtimg/201506/ufigfjvn.jpg"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201506/ufigfjvn_l.jpg""style=""width:180px;height:140px;""/></a><atitle=""每次吵架,都希望男票这样解决战斗。。。""href=""https://localhost/hvtimg/201506/iqoqrhfn.gif"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201506/iqoqrhfn_l.gif""style=""width:180px;height:140px;""/></a><atitle=""现代舞舞蹈家玛莎·葛兰姆动画Logo""href=""https://localhost/hvtimg/201506/xlyxp4wg.png"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201506/xlyxp4wg_l.png""style=""width:180px;height:140px;""/></a><atitle=""美女""href=""https://localhost/hvtimg/201506/m921jw4n.jpg"rel="externalnofollow"rel="externalnofollow""><imgsrc=""https://localhost/hvtimg/201506/m921jw4n_l.jpg""style=""width:180px;height:140px;""/></a></div>"; } privatevoidbutton_ok_Click(objectsender,EventArgse) { Regexm_hvtRegImg=newRegex(textBox_regex.Text,RegexOptions.IgnoreCase); //搜索匹配的字符串 MatchCollectionhewenqi_matches; try { hewenqi_matches=m_hvtRegImg.Matches(textBox_content.Text); //取得匹配项列表 foreach(Matchmatchinhewenqi_matches) { textBox_result.Text=textBox_result.Text+match.Groups["hewenqitext"].Value+"rn"; } } catch(Exceptionex) { textBox_result.Text=ex.ToString(); } } } } /* <divclass="dengxaing-hovertree-com"> <atitle="批量2"href=""https://localhost/hvtimg/201601/p3t2ldyr.png"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201601/p3t2ldyr_l.png""style="width:180px;height:140px;"/></a> <atitle="VS2015开发人员命令提示查看PublicKeyToken(公钥标记)"href=""https://localhost/hvtimg/201601/wn0bamxp.png"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201601/wn0bamxp_l.png""style="width:180px;height:140px;"/></a> <atitle="消灭僵尸游戏"href=""https://localhost/hvtimg/201601/q47add2k.jpg"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201601/q47add2k_l.jpg""style="width:180px;height:140px;"/></a> <atitle="纯CSS3漂亮的房子不错的天气"href=""https://localhost/hvtimg/201601/lbau8hqb.png"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201601/lbau8hqb_l.png""style="width:180px;height:140px;"/></a> <atitle="HoverClock"href=""https://localhost/hvtimg/201601/thbniyjx.png"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201601/thbniyjx_l.png""style="width:180px;height:140px;"/></a> <atitle="javascript元旦倒计时特效"href=""https://localhost/hvtimg/201512/o9qashmi.gif"rel="externalnofollow"rel="externalnofollow"> <imgsrc=""https://localhost/hvtimg/201512/o9qashmi_l.gif"style="width:180px;height:140px;"/></a> <atitle="下雨"href=""https://localhost/hvtimg/201512/f748s0ko.jpg"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201512/f748s0ko_l.jpg""style="width:180px;height:140px;"/></a> <atitle="直接说喜欢你!"href=""https://localhost/hvtimg/201512/vt1tf0xl.jpg"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201512/vt1tf0xl_l.jpg""style="width:180px;height:140px;"/></a> <atitle="IIS7,IIS7.5URL重写模块工具"href=""https://localhost/hvtimg/201512/ew4yb78b.png"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201512/ew4yb78b_l.png""style="width:180px;height:140px;"/></a> <atitle="山水画"href=""https://localhost/hvtimg/201512/agagq0or.jpg"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201512/agagq0or_l.jpg""style="width:180px;height:140px;"/></a> <atitle="刘德华"href=""https://localhost/hvtimg/201512/r51a22uy.gif"rel="externalnofollow"rel="externalnofollow"> <imgsrc=""https://localhost/hvtimg/201512/r51a22uy_l.gif"style="width:180px;height:140px;"/></a> <atitle="IIS为网站设置域名"href=""https://localhost/hvtimg/201512/69yoeuhf.jpg"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201512/69yoeuhf_l.jpg""style="width:180px;height:140px;"/></a> <atitle="HoverTreeCMS分成"href=""https://localhost/hvtimg/201512/jgi94fdf.png"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201512/jgi94fdf_l.png""style="width:180px;height:140px;"/></a> <atitle="范冰冰"href=""https://localhost/hvtimg/201511/6j9j6tk5.png"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201511/6j9j6tk5_l.png""style="width:180px;height:140px;"/></a> <atitle="周末愉快"href=""https://localhost/hvtimg/201511/60mj4wsb.gif"rel="externalnofollow"rel="externalnofollow"> <imgsrc=""https://localhost/hvtimg/201511/60mj4wsb_l.gif"style="width:180px;height:140px;"/></a> <atitle="谢霆锋手机背景图"href=""https://localhost/hvtimg/201508/cnvkv745.jpg"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201508/cnvkv745_l.jpg""style="width:180px;height:140px;"/></a> <atitle="现代舞舞蹈家玛莎·葛兰姆动画Logo"href=""https://localhost/hvtimg/201506/ufigfjvn.jpg"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201506/ufigfjvn_l.jpg""style="width:180px;height:140px;"/></a> <atitle="每次吵架,都希望男票这样解决战斗。。。"href=""https://localhost/hvtimg/201506/iqoqrhfn.gif"rel="externalnofollow"rel="externalnofollow"> <imgsrc=""https://localhost/hvtimg/201506/iqoqrhfn_l.gif"style="width:180px;height:140px;"/></a> <atitle="现代舞舞蹈家玛莎·葛兰姆动画Logo"href=""https://localhost/hvtimg/201506/xlyxp4wg.png"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201506/xlyxp4wg_l.png""style="width:180px;height:140px;"/></a> <atitle="美女"href=""https://localhost/hvtimg/201506/m921jw4n.jpg"rel="externalnofollow"rel="externalnofollow""> <imgsrc=""https://localhost/hvtimg/201506/m921jw4n_l.jpg""style="width:180px;height:140px;"/></a> </div> */
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
https://tools.jb51.net/regex/javascript
正则表达式在线生成工具:
https://tools.jb51.net/regex/create_reg
更多关于C#相关内容感兴趣的读者可查看本站专题:《WinForm控件用法上述就是C#学习教程:WinForm使用正则表达式提取内容的方法示例分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/907843.html