正则表达式复习 (?<=) (?=)
1.首先值得一说的是"<" 和">" 不是元字符 "."是元字符 ,连接字符"-",即使在字符组内部也不一定就是元字符,如果连接字符出现在字符组的开头,它表示的就是一个普通的字符,而不是一个范围如[a-zA-z0-9]里的连接字符"-",代表的就是一个范围小写字符a到z或大写字符A到Z或数字0到9,
同样的"?"和"."(问号和点通常被认为是元字符)但在字符组内部[a-zA-Z_!.]的特殊字符(元字符)就只有两个连接符("-")。
下面的图解中,第三组括号的内容被取了别名tdCell
private string getHtml()
{
try
{
date = DateTime.Now.Date.ToString("yyyy-MM-dd") + " 09:30:00";
WebClient wc = new WebClient();
//string reg = "<td(\\s+)?width=\"15%\"(\\s+)?align=\"center\"><a(\\s+)?href=\"#\".+<font(\\s+)?color=blue>(?<type>.+)(\\s+)?</font>.+(\\s+)?<td(\\s+)?width=\"35%\"(\\s+)?align=\"center\">(?<values>.+)(\\s+)?</td>(\\s+)?<td(\\s+)?width=\"10%\"(\\s+)?align=\"right\"><img(\\s+)?src=\"/shibor/web/html/images/newimages/(?<image>.+)\"(\\s+)?width=\"11\"(\\s+)?height=\"11\"(\\s+)?/>.+(\\s+)<td(\\s+)?width=\"25%\"(\\s+)?align=\"left\">(?<BP>.+)(\\s+)?</td>";
using (Stream stream = wc.OpenRead("http://www.shibor.org/shibor/ShiborTendaysShow.do"))
{
using (StreamReader sr = new StreamReader(stream, Encoding.Default))
{
string content = sr.ReadToEnd();
//单行模式匹配
//让.可以匹配到\n
string clas;
//string patern = @"(?<=<table id=""result"" cellpadding=""0"" class=""shiborquxian2"">)([\s\S]*?)(?=</table>)";
string patern = @"(?<=<table (.*)?class=""shiborquxian2""[^>]*?>)([\s\S]*?)(?=</table>)";
MatchCollection matches = Regex.Matches(content, patern);
string trs = string.Empty;
foreach (Match match in matches)
{
trs = match.Groups[0].Value;
break;
}
string trPatern = @"(?<=<tr(.*)?[^>]*?>)([\s\S]*?)(?=</tr>)";
MatchCollection trMatchCollection = Regex.Matches(trs, trPatern);
List<string> trList = new List<string>();
List<string> CurrentTrList = new List<string>();
for (int j = 0; j < trMatchCollection.Count; j++)
{
Match match = trMatchCollection[j];
string tr = string.Empty;
tr = match.Groups[0].Value;
CurrentTrList.Add(tr);
if (j == 1)
{
trList.Add(tr);
break;
}
}
//
string tdPatern = @"(?<=(<td[^>]*?>))(?<tdCell>[\s\S]*?)(?=</td>)";
MatchCollection tdMatchCollection = Regex.Matches(trList[0], tdPatern);
MatchCollection CurrenttdMatchCollection = Regex.Matches(CurrentTrList[0], tdPatern);
string td = string.Empty;
List<string> tdlList = new List<string>();
List<string> CurrenttdlList = new List<string>();
foreach (Match match in CurrenttdMatchCollection)
{
td = match.Groups["tdCell"].Value;
CurrenttdlList.Add(td);
}
foreach (Match match in tdMatchCollection)
{
td = match.Groups["tdCell"].Value;
tdlList.Add(td);
}
infoTitleW = CurrenttdlList[0];
List<string> termList = new List<string>()
{
"O/N","1W","2W","1M","2M","3M","6M","9M","1Y"
};
MKT += @"
<ul id='xianc' class='pricing_table'>
<li class='price_block'>
<h5 style='color: #ffffff;'>期限</h5>
<ul class='features'>
<li>O/N</li>
<li>1W</li>
<li>2W</li>
<li>1M</li>
<li>3M</li>
<li>6M</li>
<li>9M</li>
<li>1Y</li>
</ul>
</li>
";
MKT += @" <li class='price_block'>
<h5 style='color:#ffffff'>Shibor(%)</h5>
<ul class='features'>";
for (int i = 1; i < tdlList.Count; i++)
{
decimal prior = decimal.Parse(tdlList[i].ToString());
decimal curr = decimal.Parse(CurrenttdlList[i].ToString());
decimal change = (curr - prior) * 100;
MKT += string.Format("<li>{0}</li>", curr);
}
MKT += "</ul></li>";
MKT += @" <li class='price_block'>
<h5 style='color:#ffffff'>变化值(BP)</h5>
<ul class='features'>";
for (int i = 1; i < tdlList.Count; i++)
{
decimal prior = decimal.Parse(tdlList[i].ToString());
decimal curr = decimal.Parse(CurrenttdlList[i].ToString());
decimal change = (curr - prior) * 100;
MKT += string.Format("<li>{0}</li>", change);
}
MKT += "</ul></li>";
MKT += @" <li class='price_block'>
<h5 style='color:#ffffff'>涨跌(BP)</h5>
<ul class='features'>";
for (int i = 1; i < tdlList.Count; i++)
{
decimal prior = decimal.Parse(tdlList[i].ToString());
decimal curr = decimal.Parse(CurrenttdlList[i].ToString());
decimal change = (curr - prior) * 100;
MKT += string.Format("<li> {0} </li>", change >= 0 ? "<img height=7 src='/images/upicon.gif'>" : "<img height=7 src='/images/downicon.gif'>");
}
MKT += "</ul></li></ul>";
}
}
}
catch (Exception ex)
{
MKT = "部分内容加载失败因为:" + ex.Message + "<br/><br/>请点击 <a href='#' id='relaod' style='color:blue;'>重新加载</a>";
//Response.Write("部分内容加载失败因为:" + ex.Message + "\n请尝试重新刷新页面");
//Response.End();
// throw new Exception("部分内容加载失败因为:"+ex.Message+"\n请尝试重新刷新页面");
}
return MKT;
}
protec
正则表达式复习 (?<=) (?=)的更多相关文章
- python re 正则表达式复习
正则表达式是一种小巧的独立语言,用于字符串的匹配 一.元字符 1.. 匹配除换行符外的任意字符 2.^ 匹配字符串开头 3.$ 匹配字符串末尾 4.* 匹配前一字符n次 5.+ 匹配前一字符1-n次 ...
- java 正则表达式 复习
正则表达式在日常开发中会经常的接触到,学会了正则可以更有效的对字符进行验证.拆分.替换.判断字符串是否合法等操作... 常用语法: 字符的取值范围 1.[abc] : 表示可能是a,可能是b,也可能是 ...
- C# 正则表达式 -- 复习
符号解释: \ 特殊的字符,转义 ^ 匹配输入的字符串的开始位置 $ 匹配输入的字符串的结束位置 * 匹配0次或多次,等价于{0,} + 匹配1次或多次,等价于{1,} ? 匹配0次或1次,等价于{0 ...
- C#复习正则表达式
由于前段时间为了写工具学的太J8粗糙 加上最近一段时间太浮躁 所以静下心来复习 一遍以前学的很弱的一些地方 1 委托 public delegate double weituo(double a, d ...
- linux 下 Emacs dired 模式 隐藏 dot file ".filename" 文件
有时候 emacs 下调用 C-x dired 是挺方便的一个事,但是一堆的点文件(linux下以"."为前缀的文件,即隐藏文件)让人目不暇接,打算隐藏之. 参考如下: 最主要的是 ...
- Java爬虫初体验
年关将近,工作上该完成的都差不多了,上午闲着就接触学习了一下爬虫,抽空还把正则表达式复习了,Java的Regex和JS上还是有区别的,JS上的"\w"Java得写成"\\ ...
- 【JS复习笔记】05 正则表达式
好吧,正则表达式,我从来没记过.以前要用的时候都是网上Copy一下的. 这里还是扯一下吧,以后要是有要用到的正则表达式那么就收集到这个帖子里.(尽管我认为不会,因为我根本就不是一个专业的前端,我只是来 ...
- Web服务器-正则表达式-整理+复习(3.1.1)
@ 目录 1.常用api 2.例子 3.正则表达式模式 关于作者 参考文章 1.常用api re.match(pattern, string, flags=0) pattern 匹配的正则表达式 st ...
- [Java基础复习] -- x. 正则表达式的使用
序号待定, 先用x占位表示 理论知识待完善, 先贴上代码 import java.util.regex.Matcher; import java.util.regex.Pattern; import ...
随机推荐
- Django--django-admin.py on windows does not work
使用命令:Python django-admin.py startproject projectname 使用django-admin.py startapp myblog没有创建新的app,使用下面 ...
- linux(6)
第十五单元 软件包的管理 [本节内容]1. 使用RPM安装及移除软件(详见linux系统管理P374)1) 掌握RPM的定义:RPM就是Red Hat Package Manger(红帽软件包管理工具 ...
- Memcached数据存储方式
1. memcached的数据存储方式被称为Slab Allocator,其基本方式是: ①:先把内存分成很多Slab,这个大小是预先规定好的,已解决内存碎片的问题.分配给Slab的内存空间被称为Pa ...
- Dell PowerEdge R720内存安装原则
Dell PowerEdge R720内存安装原则 摘要:系 统包含 24 个内存插槽,分为两组(每组 12 个),每个处理器一组.每组的 12 个插槽分入四个通道.在每个通道中,第一个插槽的释放 ...
- 【转】使用JMeter进行负载测试——终极指南
使用JMeter进行负载测试——终极指南 这篇教程讨论的是JMeter,它是一款基于Java的.集合了几个应用程序.具有特定用途的负载和性能测试工具. 本篇主要涉及的内容: 解释一下JMeter的用途 ...
- JVM之运行时数据区
Java虚拟机运行时数据区包括PC寄存器.Java虚拟机栈.Java堆.方法区.本地方法栈.运行时常量池六个部分. 1. PC寄存器 PC寄存器(又叫程序计数器,Program Counter Reg ...
- spring-session之一:简介、使用及实现原理
一.背景 http session(企业)一直都是我们做集群时需要解决的一个难题,我们知道HttpSession是通过Servlet容器创建和管理的,像Tomcat/Jetty都是保存在内存中的.而如 ...
- php获取当前月月初至月末的时间戳,上个月月初至月末的时间戳
当前月 <?php $thismonth = date('m'); $thisyear = date('Y'); $startDay = $thisyear . '-' . $thismonth ...
- keepalived 预防脑裂检测脚本
1 检查vip [root@mysql2 keepalived]# cat /etc/keepalived/check_brain_keepalived.sh #!/bin/bash # 检查脑裂的脚 ...
- python‘s tenth day for me
动态参数 *args **kwargs *args 动态参数,万能参数 # args 接受的就是实参对应的 所有位置参数,并将其放在元祖中. def func(*args): pri ...