首页 > 其他 > 详细

正则表达式复习 (?<=) (?=)

时间:2015-09-13 09:17:29      阅读:197      评论:0      收藏:0      [点我收藏+]

 1.首先值得一说的是"<" 和">" 不是元字符 "."是元字符 

技术分享

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>&nbsp{0}&nbsp</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/>请点击&nbsp;&nbsp;&nbsp;<a href=‘#‘ id=‘relaod‘ style=‘color:blue;‘>重新加载</a>";
//Response.Write("部分内容加载失败因为:" + ex.Message + "\n请尝试重新刷新页面");
//Response.End();
// throw new Exception("部分内容加载失败因为:"+ex.Message+"\n请尝试重新刷新页面");
}
return MKT;
}
protec

正则表达式复习 (?<=) (?=)

原文:http://www.cnblogs.com/kexb/p/4804047.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!