Matches正则使用提取内容

用VS新建WinForm程序,窗体上是三个文本框和一个按钮。
可以自己构造正则表达式,自己修改匹配内容

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

代码如下:

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

转载自:http://hovertree.com/h/bjaf/nxoqir0b.htm

http://www.cnblogs.com/sosoft/p/kaifajishu.html

时间: 2024-10-10 14:25:11

Matches正则使用提取内容的相关文章

java 正则匹配提取html纯文本

本文来自于我的个人博客: java 正则匹配提取html纯文本 做内容的大家都知道,从html中直接提取纯文本是一个很大的问题,现将我做的正则匹配贴上: import java.util.regex.Matcher; import java.util.regex.Pattern; public class TestReg { static String reg = "<[a-zA-Z]+.*?>([\s\S]*?)</[a-zA-Z]*>"; public st

javascript 正则匹配 提取全部 preg_match_all matchAll方法

javascript 提取所有的的方法,javascript中没有matchAll这个方法. 用while来实现类似 PHP 中的preg_match_all() :(by default7#zbphp.com) <!DOCTYPE html> <html> <head> <title></title> </head> <body> <select name="AgentIdP" id="

从excel的大单元格里快速提取内容

我公司的服务器信息会保存在一份高加密的excel里,由于历史遗留问题,里面的格式节选一部分出来是这样的: 注意看,ip地址不分内网外网是放在一个大的单元格里,中间是用空格隔开的,造成了这样的视觉效果. 现在公司需要把所有的服务器重新更换到新的zookeeper,那么使用ansible在批量处理的时候,就需要提取这些服务器的内网ip地址录入到hosts文件里,但是由于服务器实在太多不可能一个一个手动从excel的单元格挑选出"内网IP地址"复制粘贴,那么就需要进行一下批量挑选内网IP地址

Visual Stuido 2017中使用正则修改部分内容

最近在项目中想实现一个小工具,需要根据类的属性<summary>的内容加上相应的[Description]特性,需要实现的效果如下 修改前: /// <summary> /// 条形码 /// </summary> public List<GoodsBarcodeEditModel> Barcodes { get; set; } 修改后: /// <summary> /// 条形码 /// </summary> [Descriptio

java String matches 正则表达

package test; /** * 在String的matches()方法,split()方法中使用正则表达式. * @author fhd001 */ public class RegexTest { public static void main(String[] args) { /* * 普通字符 */ String str1 = "abc45abc345"; String[]arr1 = str1.split("abc"); for (String st

python提取内容并写入到Excel

#! /usr/bin/env python#_*_coding:utf-8_*_ '''Created on 2017-5-12 @author: Win-1'''import re,os,timeimport xlwt #读取SNRt值def readSNRt(): hotimeList = [] SrcMacList = [] BMacList = [] timeList = [] FileOpen = open('20170521-3015_7_V175.85.log') FileMsg

C# 正则获取html内容

1.获取div内容 string str = "tt<u>ss</u><div id=\"test\"><div><a>fds</a></div></div>"; Regex reg = new Regex(@"(?is)<div\s+id=""test"">(?><div[^>]*>(

Asp.net正则获取html内容

1.获取div内容 string str = "tt<u>ss</u><div id=\"test\"><div><a>fds</a></div></div>"; Regex reg = new Regex(@"(?is)<div\s+id=""test"">(?><div[^>]*>(

php正则字符串提取汉字

/*$str 为输入.输出字符串变量*/ preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $str, $matches); $str = join('', $matches[0]); 原文地址:https://www.cnblogs.com/qingsong/p/9250968.html