正则表达式学习笔记

时间:2022-12-25 23:14:34
符号 说明 示例 备注
. 匹配除\n的任意单个字符
Regex.IsMatch("acb", "a.b");匹配成功
Regex.IsMatch("accb", "a.b");匹配失败
当.出现在中括号[]中的时候,本身就表示一个普通的.,已经不再是“元字符”了。
[] 匹配括号内任意单个字符 Regex.IsMatch(“a0b”,”a[0-9]b”)匹配成功
Regex.IsMatch(“a01b”,”a[0-9]b”)匹配失败(必须是单个字符)
 
| Regex.IsMatch("z", "z|food");匹配成功
Regex.IsMatch("d", "z|food");匹配失败
优先级低,最后才计算
() 改变优先级,提取分组 Regex.IsMatch("axb", "a(x|y)b");匹配成功
Regex.IsMatch("axxb", "a(x|y)b");匹配失败
 
{n} 表示前面的表达式必须出现n次 Regex.IsMatch("aaaa", "a{3}");匹配成功  
{n,} 表示前面的表达式至少出现n次 Regex.IsMatch("aaaa", "a{3,}");匹配成功  
{n,m} 表示前面的表达式只是至少n次,最多出现m次 Regex.IsMatch("aaaa", "a{5,6}");匹配失败
Regex.IsMatch("aaaaaaa", "a{5,6}");匹配失败
 
$
字符串的结尾
Regex.IsMatch("za", "^za");匹配成功
Regex.IsMatch("za", "^za");匹配失败
 
^ 字符串的开头 Regex.IsMatch("za", "^za");匹配成功
Regex.IsMatch("za", "^za");匹配失败
在中括号中表示非[^]
* 表示前面表达式出现多次    
+ 表示前面的表达式出现1或多次    
? 表示前面表达式出现0或1次   ?可以用来终止贪婪模式
\d 表示[0-9]   比[0-9]多的是,也可以匹配全角下的数字
\D 与\d相反,除数字外任意字符    
\s 表示空白符    
\S 与\s相反,非空白符   匹配所有字符可以用[\s\S]
\w 表示[a-zA-Z0-9]   比[a-zA-Z0-9]多的是,也可以匹配全角下的字母数字
\W 与\w相反    
\b 表示单词边界,即不是单词的字符,如逗号空格,但特殊符号如¥$#就不是单词边界    

备注:

1、|的优先级最低,需要特别注意

正则:z|food表示字符串中出现z或food都可以匹配成功

正则表达式学习笔记

2、^z|food$由于|优先级最低,所以只要以z开头或以food结尾即匹配

正则表达式学习笔记

3、^(z|food)$,括号提高了|的优先级,表示必须以z开头并结尾或,以food开头并结尾。也就是只有z和food可以匹配成功

正则表达式学习笔记

函数:

Regax.IsMatch()

regex.IsMatch表示只要整个字符串有任何一部分可以匹配该正则表达式,返回ture

由于net默认采用unicode匹配方式,所以\d也匹配全角数字,解决办法:Regex.IsMatch("", @"\w",RegexOptions.ECMAScript);用欧洲计算机的标准

案例:匹配是否为>=10并且小于等于20的数组

Regex.IsMatch(msg, @"^(1[0-9]|20)$");记得不能写成^[1-2][0-9]$,这样表示必须以1或2开头,中间不能有字符,并以0-9结尾,而29也会被匹配。

 

#region 2、判断是否是合法的日期格式“2008-08-08”。四位数字-两位数字-两位数字。 
while (true)
{
Console.WriteLine(
"请输入日期:");
string date = Console.ReadLine();
bool b = Regex.IsMatch(date, @"^\d{4}-\d{1,2}-\d{1,2}$", RegexOptions.ECMAScript);
Console.WriteLine(b);
}

#endregion
Regax.Replace()

在指定的输入字符串内,使用指定的替换字符串替换与某个正则表达式模式匹配的字符串。

msdn介绍:http://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex.replace.aspx

正则表达式学习笔记

案例:去除重复字符,只保留一个

string msg = "杨杨杨杨杨中中中中科科科科科"; 
//\1、\2、\3、...这种写法就表示“反向引用”,在字符串替换中引用分组是使用$1方式,在正则表达式内部引用分组信息是使用\1这种方式。
msg = Regex.Replace(msg, @"(.)\1+", "$1");
Console.WriteLine(msg);
Console.ReadKey();

 

Regex.match()

在输入字符串中搜索正则表达式的匹配项,并将精确结果作为单个 Match 对象返回。

一般字符串提取时不需要加^$,因为是在字符处找出一部分和该正则表达式匹配的元素,然后提取,所以不是完全匹配。

msdn里的介绍:http://technet.microsoft.com/zh-cn/library/system.text.regularexpressions.regex.match(v=vs.90).aspx

正则表达式学习笔记

案例:

从“June 26 , 1951 ”中提取出月份June、26、1951来。@"([a-zA-Z]+)\s+\d{1,2},\s*\d{4}"进行匹配。月份和日之间是必须要有空格分割的,

所以使用空白符号“\s”匹配所有的空白字符,此处的空格是必须有的,所以使用“+”标识为匹配1至多个空格。之后的“,”与年份之间的空格是可有可无的,所以使用“*”表示为匹配0至多个

string msg = "June 26 , 1951 "; 
Match match
= Regex.Match(msg, @"^([a-zA-Z]+)\s*(\d{1,2})\s*,\s*(\d{4})\s*$");
Console.WriteLine(match.Groups[
1].Value);
Console.WriteLine(match.Groups[
2].Value);
Console.WriteLine(match.Groups[
3].Value);
Console.ReadKey();
 
Regex.Matches():

在输入字符串中搜索正则表达式的所有匹配项并返回所有匹配。

msdn里的介绍:http://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex.matches.aspx

正则表达式学习笔记

案例:

正则表达式学习笔记正则表达式学习笔记大家留下email交友吧_email_天涯社区.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0071)http://laiba.tianya.cn/laiba/CommMsgs?cmm=13803&tid=2551054490417499901 -->
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<link href="./大家留下email交友吧_email_天涯社区_files/nav.css" rel="stylesheet" type="text/css">
<link href="./大家留下email交友吧_email_天涯社区_files/general.css" rel="stylesheet" type="text/css">
<link href="./大家留下email交友吧_email_天涯社区_files/bulou.css" rel="stylesheet" type="text/css">

<style>
IMG{
max
-width:960px;
width:expression(
this.width>960?"960px":true);
overflow:hidden;
}
</style>






<script type="text/javascript" charset="utf-8" src="./大家留下email交友吧_email_天涯社区_files/global-all.js"></script><img src="./大家留下email交友吧_email_天涯社区_files/guid.jsp" height="0" width="0"><script defer="" src="./大家留下email交友吧_email_天涯社区_files/cityid.jsp"></script><img src="./大家留下email交友吧_email_天涯社区_files/clickAnalyse.jsp" height="0" width="0"></head><body><div id="tb_show2_mask" style="display:none"></div><div id="tb_show2_box" style="display:none"></div>






<script type="text/javascript" charset="utf-8" src="./大家留下email交友吧_email_天涯社区_files/groupCommon.js"></script>

<script type="text/javascript">
function outsideShow(s, id){
var outside=document.getElementById(id);
if (s=="show") {
outside.style.display
="block";}
else{
outside.style.display
="none";}

}

//bbs跳转分页
function GoPage(){
var pageCount=2;
var pageIds=document.getElementsByName("pageId");
var curPageNo=1;
if(pageIds.length>1){
curPageNo
=pageIds[0].value;
if(isNaN(curPageNo)||curPageNo==''){
curPageNo
=pageIds[1].value;
}
}
if(isNaN(curPageNo) || curPageNo<1){
curPageNo
=1;
}
else if(curPageNo>pageCount){
curPageNo
=pageCount;
}
window.location
="showArticle.jsp?groupId=93803&articleId=255105449041749990113803&curPageNo="+curPageNo;
}
/*权限判断:来吧助理以上的成员,系统管理员,有数据权限的用户,非权限用户不显示js*/


</script>
<script src="./大家留下email交友吧_email_天涯社区_files/hi.htm"></script>
<meta name="keywords" content="IT,互联网,来吧,圈子,群组,天涯,社区">
<meta name="description" content="女岁。天涯来吧,全球华人网上家园">
<title>大家留下email交友吧_email_天涯社区</title>



<center>

<!-- 全局上导航开始 -->
<!--<script type="text/javascript" charset="utf-8" src="http://global.tianyaui.com/global/gnav/js/nav_top.js"></script>-->
<!-- 全局上导航结束 -->
<div class="wrapper">
<!-- 页面主体区-开始 -->
<div id="mainDiv">

<div class="body">

<script language="javascript" src="./大家留下email交友吧_email_天涯社区_files/tianyaTopGuideJs100percent.js"></script><style type="text/css"><!-- #tianyatopguide_nav {margin-top:2px; font-size:12px; border-bottom:4px solid #E3E3E3; height:45px; }#tianyatopguide_nav .tianyatopguidenavlogo{float:left;height:35px;width:95px}#tianyatopguide_nav .tianyatopguidenavl{float:left;height:35px;padding-top:8px;text-align:left;color:#646464;line-height:18px;font-size:12px; }#tianyatopguide_nav .tianyatopguidenavl a{ text-decoration:none;color:#777; }#tianyatopguide_nav .tianyatopguidenavl a:hover{ color:#ff6600;text-decoration:none;}#tianyatopguide_nav .tianyatopguidenavr{float:right;height:35px;text-align:right;color:#646464;line-height:18px;font-size:12px;padding-top:8px; }#tianyatopguide_nav .tianyatopguidenavr a{ text-decoration:none;color:#777;font-size:12px; }#tianyatopguide_nav .tianyatopguidenavr a:hover{ color:#ff6600;text-decoration:none;font-size:12px; }.clear { clear:both}#tianyatopguide_nav .tianyatopguidenavl ,#tianyatopguide_nav .tianyatopguidenavl a ,#tianyatopguide_nav .tianyatopguidenavl a:hover,#tianyatopguide_nav .tianyatopguidenavr ,#tianyatopguide_nav .tianyatopguidenavr a ,#tianyatopguide_nav .tianyatopguidenavr a:hover {font-size:12px;line-height:18px;font-family: &#x5b8b;&#x4f53;;}#tianyatopguideloginlayer .tianyatopguidelogtab {border:#AEC6DE 4px solid ; background:#ffffff; font-size:14px; text-align:left;padding:4px;} .clear {clear:both} --></style><div align="center"><div id="tianyatopguide_nav"><div class="tianyatopguidenavlogo"><a href="http://www.tianya.cn/" target="_blank"><img src="./大家留下email交友吧_email_天涯社区_files/tyguidejslogov.gif" border="0"></a></div> <div class="tianyatopguidenavl"><a href="http://t.tianya.cn/" target="_blank"><font color="red">天涯微博</font></a>|<a href="http://www.tianya.cn/bbs/index.shtml" target="_blank">论坛</a>|<a href="http://laiba.tianya.cn/" target="_blank"><font color="red">来吧</font></a>|<a href="http://blog.tianya.cn/" target="_blank">博客</a>|<a href="http://game.tianya.cn/" target="_blank"><font color="red">游戏</font></a>|<a href="http://pic.tianya.cn/" target="_blank">相册</a>|<a href="http://wenda.tianya.cn/" target="_blank"><font color="red">问答</font></a>|<a href="http://city.tianya.cn/" target="_blank">城市</a>|<a href="http://pinpai.tianya.cn/" target="_blank">品牌</a>|<a href="http://mall.tianya.cn/" target="_blank">购物街</a>|<a href="http://info.tianya.cn/" target="_blank"><font color="red">分类信息</font></a>|<a href="http://wap.tianya.cn/web/" target="_blank"><font color="red">掌中天涯</font></a><br><a href="http://focus.tianya.cn/" target="_blank">天涯聚焦</a>|<a href="http://news.tianya.cn/" target="_blank">社会</a>|<a href="http://depth.tianya.cn/" target="_blank">深度</a>|<a href="http://ebook.tianya.cn/" target="_blank">文学</a>|<a href="http://biz.tianya.cn/" target="_blank">财经</a>|<a href="http://auto.tianya.cn/" target="_blank">汽车</a>|<a href="http://lady.tianya.cn/" target="_blank"><font color="red">女人</font></a>|<a href="http://fashion.tianya.cn/" target="_blank">时尚</a>|<a href="http://baby.tianya.cn/" target="_blank">亲子</a>|<a href="http://emo.tianya.cn/" target="_blank">情感</a>|<a href="http://ent.tianya.cn/" target="_blank">娱乐</a>|<a href="http://sports.tianya.cn/" target="_blank">体育</a>|<a href="http://travel.tianya.cn/" target="_blank">旅游</a>|<a href="http://it.tianya.cn/" target="_blank">IT</a>|<a href="http://digi.tianya.cn/" target="_blank">数码</a>|<a href="http://pp.tianya.cn/" target="_blank">图片</a>|<a href="http://video.tianya.cn/" target="_blank">视频</a>|<a href="http://event.tianya.cn/" target="_blank">活动</a>|<a href="http://try.tianya.cn/" target="_blank">体验</a></div><div class="tianyatopguidenavr"><a href="http://laiba.tianya.cn/laiba/CommMsgs?cmm=13803&tid=2551054490417499901#" onclick="MenuUtil.loginIfr();return false;" title="社区登录"><font color="red"><b>登录</b></font></a> | <a href="http://passport.tianya.cn/register/default.jsp?sourceURL=http%3A//www.tianya.cn/bbs/" target="_blank"><font color="red"><b>注册</b></font></a><br><a href="http://service.tianya.cn/" target="_blank"><font color="#6699CC">帮助</font></a></div></div></div><div class="clear"></div>

<form method="post" id="form1" name="form1" action="http://laiba.tianya.cn/tribe/showArticle.jsp">
<input type="hidden" name="idwriter" value="0">
<input type="hidden" name="groupId" value="93803">
<input type="hidden" name="articleId" value="255105449041749990113803">
<input type="hidden" name="curPageNo" value="1">
<input type="hidden" name="hiddenState" id="hiddenState" value="1">
<div class="article_box">


<div id="nav_trail" class="bottomline_dashed" style="background-color: #F5F9FA">
<ul>
<li>
<a href="http://groups.tianya.cn/">天涯来吧 </a>
</li>
<li>
<a href="http://13803.groups.tianya.cn/">email</a>
</li>
<li>
<a href="http://laiba.tianya.cn/laiba/discussionArea.jsp?groupId=93803">讨论区</a>
</li>
<li class="nav_trail_last ">
本帖
</li>
</ul>
</div>

<div id="firstAuthor" class="mtime" style="word-wrap: break-word;">

<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">

<tbody><tr>
<td align="right">
<div class="share">
<ul id="share">

<li style="width:90px;"><a class="toshare" href="http://laiba.tianya.cn/laiba/CommMsgs?cmm=13803&tid=2551054490417499901#" onmouseover="outsideShow(&#39;show&#39;, &#39;outside&#39;);return false;" onmouseout="outsideShow(&#39;hidden&#39;, &#39;outside&#39;);return false;">分享到外站</a>
<div class="webs" id="outside" onmouseover="outsideShow(&#39;show&#39;, &#39;outside&#39;);return false;" onmouseout="outsideShow(&#39;hidden&#39;, &#39;outside&#39;);return false;" style="display: none; ">
<span><a class="tencent rline" href="javascript:(function(){window.open('http://v.t.qq.com/share/share.php?title='+encodeURI(document.title)+'&url='+encodeURIComponent(document.location)+'&appkey='+encodeURI('c0b16ae5ed7f4d6599739d2a2cbb9175')+'&site='+''+'&pic='+encodeURI('')+'','_blank','width=700, height=680, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no');})()">腾讯微博</a></span>
<span><a class="digu" href="javascript:(function(){window.open('http://www.diguff.com/diguShare/bookMark_FF.jsp?title='+encodeURIComponent(document.title)+'&url='+encodeURIComponent(location.href),'_blank','width=580,height=310');})()">嘀咕</a></span>
<span><a class="sinat rline" href="javascript:void((function(s,d,e,r,l,p,t,z,c){x=document;y=window;if(x.selection){Q=x.selection.createRange().text;}else%20if(y.getSelection){Q=y.getSelection();}else%20if(x.getSelection){Q=x.getSelection();};var%20f='http://v.t.sina.com.cn/share/share.php?',u=z||d.location,p=['url=',e(u)+e('\n')+e(Q),'&title=',e(t||d.title),'&source=',e(r),'&sourceUrl=',e(l),'&content=',c||'gb2312','&pic=',e(p||'')].join('');function%20a(){if(!window.open([f,p].join(''),'mb',['toolbar=0,status=0,resizable=1,width=440,height=430,left=',(s.width-440)/2,',top=',(s.height-430)/2].join('')))u.href=[f,p].join('');};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else%20a();})(screen,document,encodeURIComponent,'','','','','','utf-8'));">新浪微博</a></span>
<span><a class="douban" href="javascript:var u='http://www.douban.com/recommend/?url='+location.href+'&title='+encodeURIComponent(document.title);window.open(u,'douban','toolbar=0,resizable=1,scrollbars=yes,status=1,width=450,height=330');void(0)">豆瓣</a></span>
<span><a class="sohu rline" href="javascript:void((function(s,d,e,r,l,p,t,z,c){var f='http://t.sohu.com/third/post.jsp?',u=z||d.location,p=['&url=',e(u),'&title=',e(t||d.title),'&content=',c||'gb2312','&pic=',e(p||'')].join('');function%20a(){if(!window.open([f,p].join(''),'mb',['toolbar=0,status=0,resizable=1,width=660,height=470,left=',(s.width-660)/2,',top=',(s.height-470)/2].join('')))u.href=[f,p].join('');};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else%20a();})(screen,document,encodeURIComponent,'','','','','','utf-8'));">搜狐微博</a></span>
<span><a class="kaixin" href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(kaixin=window.open('http://www.kaixin001.com/~repaste/repaste.php?&rurl='+escape(d.location.href)+'&rtitle='+escape(d.title)+'&rcontent='+escape(d.title),'kaixin'));kaixin.focus();">开心网</a></span>
<span><a class="qq rline" href="javascript:window.open('http://shuqian.qq.com/post?from=3&title='+encodeURIComponent(document.title)+'&uri='+encodeURIComponent(document.location.href)+'&jumpback=2&noui=1','favit','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');void(0)">QQ书签</a></span>
<span><a class="renren" href="javascript:void((function(s,d,e){if(/xiaonei\.com/.test(d.location))return;var%20f='http://share.xiaonei.com/share/buttonshare.do?link=',u=d.location,l=d.title,p=[e(u),'&title=',e(l)].join('');function%20a(){if(!window.open([f,p].join(''),'xnshare',['toolbar=0,status=0,resizable=1,width=626,height=436,left=',(s.width-626)/2,',top=',(s.height-436)/2].join('')))u.href=[f,p].join('');};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else%20a();})(screen,document,encodeURIComponent));">人人网</a></span>
<span><a class="baidu rline" href="javascript:u=location.href;t=document.title;c%20=%20""%20+%20(window.getSelection%20? window.getSelection() : document.getSelection ? document.getSelection() : document.selection.createRange().text);var url="http://cang.baidu.com/do/add?it="+encodeURIComponent(t)+"&iu="+encodeURIComponent(u)+"&dc="+encodeURIComponent(c)+"&fr=ien#nw=1";window.open(url,"_blank","scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes"); void 0">百度搜藏</a></span>
<span><a class="bai" href="javascript:location.href='http://bai.sohu.com/app/share/blank/add.do?link='+location.href">白社会</a></span>
<span><a class="yahoo rline" href="http://laiba.tianya.cn/laiba/CommMsgs?cmm=13803&tid=2551054490417499901#" onclick="window.open(&#39;http://myweb.cn.yahoo.com/popadd.html?url=&#39;+encodeURIComponent(document.location.href)+&#39;&amp;title=&#39;+encodeURIComponent(document.title), &#39;Yahoo&#39;,&#39;scrollbars=yes,width=780,height=455,left=80,top=80,status=yes,resizable=yes&#39;);return false;">雅虎收藏</a></span>
<span><a class="live" href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(blog=window.open('https://favorites.live.com/quickadd.aspx?url='+escape(d.title)+'&newspath='+escape(d.location.href)+'&subtitle='+escape(t),'Live'));blog.focus();">MSN空间</a></span>
<div class="clear"></div>
</div>

</li>
</ul>
</div>

</td>
</tr>

<tr>
<!-- -帖子标题 -->
<td align="center">
<table style="margin-top: 15px;" cellspacing="2" cellpadding="2" bordercolor="#ffffff" border="0" align="center">
<tbody>
<tr>
<td class="mtitle" bgcolor="#336699" align="center">
<div style="word-wrap: break-word;">
<font size="+1" style="color:#FFFFFF">

<a target="_top" style=" font-weight:bold" href="http://13803.groups.tianya.cn/"><font style="color:#FFFFFF">email</font>
</a>

<span id="title" style="color:#FFFFFF">大家留下email交友吧</span>
</font>
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody></table>

<table bgcolor="#F5F9FA" width="100%">
<tbody><tr style="font-size:14px">
<td id="pageDivTop">
<div class="page_number">
共2页
&nbsp; <span style="color:#0000FF">1</span> <a href="http://laiba.tianya.cn/tribe/showArticle.jsp?groupId=93803&articleId=255105449041749990113803&curPageNo=2" class="b1">2</a> <a href="http://laiba.tianya.cn/tribe/showArticle.jsp?groupId=93803&articleId=255105449041749990113803&curPageNo=2&h=n_1254814140000">下一页</a> <a href="http://laiba.tianya.cn/tribe/showArticle.jsp?groupId=93803&articleId=255105449041749990113803&curPageNo=2">末页</a> [<a href="http://laiba.tianya.cn/tribe/showArticle.jsp?groupId=93803&articleId=255105449041749990113803&curPageNo=2#bottom">回复此帖</a>]
</div>
</td>
</tr>
</tbody></table>

</div>

<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="195" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<img src="./大家留下email交友吧_email_天涯社区_files/blue_face.gif">
&nbsp;
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/13316515" class="tz_auto" target="_blank">ktery</a><span value="13316515" name="userid_13316515" style="float: none; display: inline;"></span>&nbsp;提交日期:2007-08-24 18:48:00&nbsp;访问:8798&nbsp;回复:132</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right" style="width:35px;"><font color="#999999">楼主</font></td>
</tr>
</tbody></table>
<div id="content0" class="content">
&nbsp;女 &nbsp;true188@gmail.com &nbsp;&nbsp;20岁
<div class="fs-newline"></div>
</div>
<div class="content">
<p style="background: none repeat scroll 0% 0% rgb(238, 238, 238);" id="my_tomy_p">
<a href="javascript:void(0)" class="button-my-follow" onclick="__my.action.follow(&#39;http://my.tianya.cn/interface/recommend/follow.jsp?act=addfollowbypop&amp;username=ktery&#39;)">关注楼主</a>
<a href="javascript:void(0)" class="button-my-collect" onclick="__my.action.collect(&#39;http://my.tianya.cn/interface/user/aritcleCollect.jsp?act=addcollectbypop&amp;type=%E6%9D%A5%E5%90%A7&amp;title=%E5%A4%A7%E5%AE%B6%E7%95%99%E4%B8%8Bemail%E4%BA%A4%E5%8F%8B%E5%90%A7&amp;url=http%3A%2F%2Flaiba.tianya.cn%2Ftribe%2FshowArticle.jsp%3FgroupId%3D93803%26articleId%3D255105449041749990113803&amp;head=email&amp;uid=&amp;uname=&#39;)">收藏</a>
<a href="javascript:void(0)" class="button-my-share" onclick="__my.action.share(&#39;&amp;url=http%3A%2F%2Flaiba.tianya.cn%2Ftribe%2FshowArticle.jsp%3FgroupId%3D93803%26articleId%3D255105449041749990113803&amp;title=%E5%A4%A7%E5%AE%B6%E7%95%99%E4%B8%8Bemail%E4%BA%A4%E5%8F%8B%E5%90%A7&amp;uname=ktery&amp;uid=13316515&amp;from=%E5%A4%A9%E6%B6%AF%E6%9D%A5%E5%90%A7&amp;charset=utf8&#39;)">转发至天涯微博</a>
</p>
</div>




<div id="ReContent2589394907554643968" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-03-18 10:09:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">1#</td>
</tr>
</tbody></table>
<div id="content2589394907554643968" class="content"> enc.succeed@gmail.com
</div>
</div>

<div id="ReContent2592635225314511790" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-04-04 21:17:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">2#</td>
</tr>
</tbody></table>
<div id="content2592635225314511790" class="content"> shangdonglsk@126.com
</div>
</div>

<div id="ReContent2594804024842131180" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-04-16 13:49:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">3#</td>
</tr>
</tbody></table>
<div id="content2594804024842131180" class="content"> xxxwuxiaoyyy@126.com
</div>
</div>

<div id="ReContent2598213229032941240" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-05-04 22:48:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">4#</td>
</tr>
</tbody></table>
<div id="content2598213229032941240" class="content"> kyle.qiao@gmail.com
</div>
</div>

<div id="ReContent2601138382351804551" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-05-20 17:10:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">5#</td>
</tr>
</tbody></table>
<div id="content2601138382351804551" class="content"> erniu.zw@163.com
<div class="fs-newline"></div> <div class="fs-newline"></div>叫我二牛就成了,男,26岁了
<div class="fs-newline"></div>喜欢结交天南地北的朋友
</div>
</div>

<div id="ReContent2601150278141395442" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/16787047" class="tz_auto" target="_blank">smakye</a><span value="16787047" name="userid_16787047" style="float: none; display: inline; "><a href="http://807.tianya.cn/count?t=EsHTQDHjb9uCrdbqZrng&backurl=http://passport.tianya.cn/portect?action=smsintro&from=successtiezitubiao" title="手机认证用户" target="_blank"><img border="0" align="absmiddle" src="./大家留下email交友吧_email_天涯社区_files/1291094730345.gif" width="16" height="16"></a></span>&nbsp;提交日期:2008-05-20 18:43:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">6#</td>
</tr>
</tbody></table>
<div id="content2601150278141395442" class="content"> 怎么全是匿名?
<div class="fs-newline"></div>我都有点不敢写了~
<div class="fs-newline"></div>gsh@w.cn
<div class="fs-newline"></div>(windows live 的邮箱,和hotmail一样)
</div>
</div>

<div id="ReContent2603786782140802015" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-06-03 23:45:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">7#</td>
</tr>
</tbody></table>
<div id="content2603786782140802015" class="content"> 此男生猛,生人勿近(:)开玩笑)uprich@sohu.com
</div>
</div>

<div id="ReContent2605037186691209042" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-06-10 17:29:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">8#</td>
</tr>
</tbody></table>
<div id="content2605037186691209042" class="content"> ericluj@sina.com 男 22岁
</div>
</div>

<div id="ReContent2613158912077871924" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-07-24 12:02:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">9#</td>
</tr>
</tbody></table>
<div id="content2613158912077871924" class="content"> 你好:
<div class="fs-newline"></div> &nbsp;&nbsp;&nbsp;&nbsp;我是刚毕业的大学生,很想交朋友,only普通朋友
<div class="fs-newline"></div>     www.cinderlla@163.com .
<div class="fs-newline"></div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wodengnimen
<div class="fs-newline"></div>
</div>
</div>

<div id="ReContent2615414444092651400" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-08-05 15:47:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">10#</td>
</tr>
</tbody></table>
<div id="content2615414444092651400" class="content"> coolcliff01@yahoo.com.tw
</div>
</div>

<div id="ReContent2615566797579444033" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/17052816" class="tz_auto" target="_blank">yanyufei0918</a><span value="17052816" name="userid_17052816" style="float: none; display: inline; "><a href="http://807.tianya.cn/count?t=EsHTQDHjb9uCrdbqZrng&backurl=http://passport.tianya.cn/portect?action=smsintro&from=successtiezitubiao" title="手机认证用户" target="_blank"><img border="0" align="absmiddle" src="./大家留下email交友吧_email_天涯社区_files/1291094730345.gif" width="16" height="16"></a></span>&nbsp;提交日期:2008-08-06 11:30:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">11#</td>
</tr>
</tbody></table>
<div id="content2615566797579444033" class="content"> 本人一哥哥 yanyufei0918@163.com
</div>
</div>

<div id="ReContent2617277774718364958" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-08-15 16:48:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">12#</td>
</tr>
</tbody></table>
<div id="content2617277774718364958" class="content"> wbxxny@gmail.com
<div class="fs-newline"></div> <div class="fs-newline"></div>年龄:26
<div class="fs-newline"></div>性别:男
<div class="fs-newline"></div>地区:甘肃
<div class="fs-newline"></div>qq太无趣,希望可以通过e_mail交流。有意者发!
</div>
</div>

<div id="ReContent2619165612932635701" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-08-25 21:00:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">13#</td>
</tr>
</tbody></table>
<div id="content2619165612932635701" class="content"> 对楼上的话深表赞同
<div class="fs-newline"></div>aprmch@gmail.com
</div>
</div>

<div id="ReContent2620019554840699175" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-08-30 11:27:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">14#</td>
</tr>
</tbody></table>
<div id="content2620019554840699175" class="content"> YLX857857857@163.COM &nbsp;男 交朋友
</div>
</div>

<div id="ReContent2621003016312279969" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-09-04 18:40:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">15#</td>
</tr>
</tbody></table>
<div id="content2621003016312279969" class="content"> x4long@163.com
</div>
</div>

<div id="ReContent2621003196703169702" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/39043556" class="tz_auto" target="_blank">来吧路人甲</a><span value="39043556" name="userid_39043556" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-09-04 18:42:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td>
<td align="right">16#</td>
</tr>
</tbody></table>
<div id="content2621003196703169702" class="content"> x4long@163.com
<div class="fs-newline"></div> <div class="fs-newline"></div>男 <div class="fs-newline"></div> <div class="fs-newline"></div>想找一个聊天的好朋友
<div class="fs-newline"></div> <div class="fs-newline"></div>不是搞对象 <div class="fs-newline"></div> <div class="fs-newline"></div>本人平时一贯很低调
</div>
</div>

<div id="ReContent2622355336988423044" style="word-wrap: break-word;">
<table cellspacing="0" border="0" bgcolor="#F5F9FA" width="100%">
<tbody><tr>
<td width="206" valign="bottom" align="right"></td>
<td width="500" align="center"><font size="-1" color="green">
<font color="green" size="-1">作者:<a href="http://my.tianya.cn/13939472" class="tz_auto" target="_blank">tony_ly</a><span value="13939472" name="userid_13939472" style="float: none; display: inline;"></span>&nbsp;提交日期:2008-09-12 01:36:00&nbsp;</font>
</font></td>
<td width="206" align="right" style="padding-right:10px;">

</td

 

#region 从大家留下email交友吧_email_天涯社区.htm文件中提取Email

string html = File.ReadAllText("大家留下email交友吧_email_天涯社区.htm");
//提取组:1.在正则表达式中添加()来分组。2.通过Match对象的Groups集合来获取分组信息,注意Groups的索引0表示本次匹配到的字符串,从1开始才表示组的信息。

MatchCollection matches
= Regex.Matches(html, @"([-a-zA-Z0-9_.]+)@([-a-zA-Z0-9]+(\.[a-zA-Z]+){1,2})");
//正则表达式中的每个()都具有两重含义:1.改变优先级2.提取组。
foreach (Match item in matches)
{
if (item.Success)
{
//item.Value等同于item.Groups[0].Value,所以提取组的信息应该从Groups[1]开始
Console.WriteLine(item.Value + "\t" + item.Groups[1].Value + "\t" + item.Groups[2].Value);
}
}
Console.WriteLine(matches.Count);
Console.ReadKey();
#endregion

结果:正则表达式学习笔记

 

贪婪模式:

          贪婪模式:当出现限定符+、*、?、{n,}类似这样的限定符时,正则表达式默认会尽可能多的进行匹配,这种方式叫做:”贪婪模式“。 在”限定符“后加?就表是”终止贪婪模式“。”终止贪婪模式“意味着尽可能少的匹配。

#region 贪婪模式造成的问题
Console.WriteLine(
"默认贪婪模式下的结果");
string msg = "1111。11。111。111111。";
Match match
= Regex.Match(msg, ".+");
Console.WriteLine(match.Value);
Console.WriteLine(
"禁止贪婪模式下的结果");
string msg1 = "1111。11。111。111111。";
Match match1
= Regex.Match(msg1, ".+?");
Console.WriteLine(match1.Value);
Console.ReadKey();

#endregion

正则表达式学习笔记

多行模式:

反向引用:\1 \2 \3这种写法就表示反向引用,在字符串替换中引用分组是使用$,在正则表达式内部引用分组信息则用\1这种方

环视:

?=肯定正向环视,?!否定正向环视,?<=肯定逆向环视,?<!否定逆向环视。

(?<!\d)\d{5}(?!\d)表示数字前后都没有数字,中间有五位数字,记得加括号

案例:通过“环视”来实现,提取5位电话号码

string msg = "苏坤:18999141265蒋坤:13111111111杨中科:13488888888电信:10000移动:10086联通:10010工行:95588传智播客010-82935100传智播客:010-82935150"; 
MatchCollection matches
= Regex.Matches(msg, @"(?<!\d)\d{5}(?!\d)", RegexOptions.ECMAScript);
foreach (Match item in matches)
{
Console.WriteLine(item.Value);
//Console.WriteLine(item.Groups[1].Value);
}
Console.ReadKey();