正则表达式

来源:互联网 发布:淘宝二手雷蛇北海巨妖 编辑:程序博客网 时间:2024/05/17 18:17


<div class="postText">
  <div id="cnblogs_post_body"><h3>一 前言</h3>
<p class="p0">  对于正则表达式,相信很多人都知道,但是很多人的第一感觉就是难学,因为看第一眼时,觉得完全没有规律可寻,而且全是一堆各种各样的特殊符号,完全不知所云。</p>
<p class="p0">其实只是对正则不了解而以,了解了你就会发现,原来就这样啊正则所用的相关字符其实不多,也不难记,更不难懂,唯一难的就是组合起来之后,可读性比较差,而且不容易理解,本文旨在让大家对正则有一个基本的了解,能看得懂简单的正则表达式,写得出简单的正则表达式,用以满足日常开发中的需求即可。</p>
<p class="p0">0\d{2}-\d{8}|0\d{3}-\d{7} 先来一段正则,如果你对正则不了解,是不是完全不知道这一串字符是什么意思?这不要紧文章会详细解释每个字符的含义的。</p>
<p class="p0">&nbsp;</p>
<p class="p0"><span style="font-size: 16px;"><strong>1.1 什么是正则表达式</strong></span></p>
<p class="p0">&nbsp; &nbsp; &nbsp;正则表达式是一种特殊的字符串模式,用于匹配一组字符串,就好比用模具做产品,而正则就是这个模具,定义一种规则去匹配符合规则的字符。</p>
<p class="p0"><strong><span style="font-size: 16px;">1.2 常用的正则匹配工具&nbsp;</span></strong></p>

<p class="p0">&nbsp;&nbsp;&nbsp;在线教程:</p>

<p class="p0">  1&nbsp;http://deerchao.net/tutorials/regex/regex.htm&nbsp;</p>

<p class="p0">  1&nbsp;http://www.codeyyy.com/regex/index.html&nbsp;</p>
<p class="p0">&nbsp; &nbsp; &nbsp;在线匹配工具:</p>
<p class="p0">  1&nbsp;http://www.regexpal.com/&nbsp;</p>
<p class="p0">&nbsp; &nbsp; &nbsp; 2 http://rubular.com/&nbsp;</p>
<p class="p0">&nbsp; &nbsp; &nbsp;正则匹配软件</p>
<p class="p0">&nbsp; &nbsp;<span style="color: rgb(0, 0, 255);">&nbsp; </span><span style="color: rgb(255, 0, 0);">&nbsp;<a href="http://pan.baidu.com/s/19Yn49"><span style="color: rgb(255, 0, 0);">McTracer</span></a>&nbsp;</span></p>
<p class="p0">&nbsp; &nbsp; &nbsp; 用过几个之后还是觉得这个是最好用的,支持将正则导成对应的语言如java C# js等还帮你转义了,Copy直接用就行了很方便,另外支持把正则表达式用法解释,如哪一段是捕获分组,哪段是贪婪匹配等等,总之用起来 So Happy .</p>
<p class="p0">&nbsp;</p>
<h3>二 正则字符简单介绍</h3>
<p class="p0"><strong><span style="font-size: 16px;">2.1 元字符介绍</span></strong></p>
<p class="p0">&nbsp; &nbsp;<strong>"^"</strong> :^会匹配行或者字符串的起始位置,有时还会匹配整个文档的起始位置。&nbsp;</p>
<p class="p0">&nbsp; &nbsp;<strong>"$"</strong> &nbsp;:$会匹配行或字符串的结尾</p>
<p class="p0">&nbsp; &nbsp; 如图</p>
<p class="p0"><span style="color: rgb(255, 0, 0);">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;而且被匹配的字符必须是以This开头有空格也不行,必须以Regex结尾,也不能有空格与其它字符</span></p>
<p class="p0"><img alt="" src="http://images.cnitblog.com/blog/366784/201311/23112422-56df0ef3f54c42abb55e7cabd5278824.png">&nbsp; &nbsp; &nbsp;<img alt="" src="http://images.cnitblog.com/blog/366784/201311/23112601-f5e9f5c0ddd7417eb28ac7d0734f8b8f.png"></p>
<p class="p0">&nbsp;</p>
<p class="p0">&nbsp;<strong>"\b"</strong> :不会消耗任何字符只匹配一个位置,常用于匹配单词边界 如 我想从字符串中"This is Regex"匹配单独的单词 "is" 正则就要写成 "\bis\b" &nbsp;</p>
<p class="p0">   &nbsp;\b 不会匹配is 两边的字符,但它会识别is 两边是否为单词的边界&nbsp;</p>
<p class="p0">&nbsp;<strong>"\d"</strong>: 匹配数字,</p>
<p class="p0">   &nbsp;例如要匹配一个固定格式的电话号码以0开头前4位后7位,如0737-5686123 &nbsp;正则:^0\d\d\d-\d\d\d\d\d\d\d$ 这里只是为了介绍"\d"字符,实际上有更好的写法会在 &nbsp; &nbsp; 下面介绍。</p>
<p class="p0">&nbsp;<strong>"\w"</strong>:<span>匹配字母,数字,下划线.</span></p>
<p class="p0"><span>   &nbsp;例如我要匹配"a2345BCD__TTz" 正则:"\w+" &nbsp;这里的"+"字符为一个量词指重复的次数,稍后会详细介绍。</span></p>
<p class="p0">&nbsp;<strong>"\s"</strong>:匹配空格&nbsp;</p>
<p class="p0">   &nbsp;例如字符 "a b c" 正则:"\w\s\w\s\w" &nbsp;一个字符后跟一个空格,如有字符间有多个空格直接把"\s" 写成 "\s+" 让空格重复</p>
<p class="p0">&nbsp;<strong> "."</strong>:<span>匹配除了换行符以外的任何字符 </span></p>
<p class="p0"><span>   &nbsp;这个算是"\w"的加强版了"\w"不能匹配 空格 如果把字符串加上空格用"\w"就受限了,看下用 "."是如何匹配字符"a23 4 5 B C D__TTz" &nbsp;正则:".+"</span></p>
<p class="p0"><span>&nbsp; <strong>"[abc]"</strong>: 字符组 &nbsp;匹配包含括号内元素的字符&nbsp;</span></p>
<p class="p0"><span>&nbsp; &nbsp; &nbsp; &nbsp; 这个比较简单了只匹配括号内存在的字符,还可以写成[a-z]匹配a至z的所以字母就等于可以用来控制只能输入英文了,</span></p>
<p class="p0">&nbsp;</p>
<p class="p0"><span style="font-size: 16px;"><strong>2.2 几种反义</strong></span></p>
<p class="p0"><span>  写法很简单改成大写就行了,意思与原来的相反,这里就不举例子了</span></p>
<p class="p0"><span>&nbsp; &nbsp;<strong>"</strong><span><strong>\W"</strong> &nbsp;&nbsp;</span></span>匹配任意不是字母,数字,下划线 的字符</p>
<p class="p0"><span><span>&nbsp; &nbsp;<strong>"\S"</strong> &nbsp;&nbsp;</span></span>匹配任意不是空白符的字符</p>
<p class="p0"><span><span><strong> "\D"</strong> &nbsp;</span></span>匹配任意非数字的字符</p>
<p class="p0"><span><span>&nbsp; <strong>&nbsp;"\B"</strong> &nbsp;</span></span>匹配不是单词开头或结束的位置</p>
<p class="p0"><span><span>&nbsp; &nbsp;<strong>"[^abc]"</strong> &nbsp;</span></span>匹配除了abc以外的任意字符</p>
<p class="p0">&nbsp;</p>
<p class="p0">&nbsp;<strong><span style="font-size: 16px;">2.3 &nbsp;量词</span></strong></p>
<p class="p0">  先解释关于量词所涉及到的重要的三个概念</p>
<p class="p0">&nbsp; &nbsp; 贪婪(贪心) 如"*"字符 贪婪量词会首先匹配整个字符串,尝试匹配时,它会选定尽可能多的内容,如果 失败则回退一个字符,然后再次尝试回退的过程就叫做回溯,它会每次回退一个字符,直到找到匹配的内容或者没有字符可以回退。相比下面两种贪婪量词对资源的消耗是最大的,</p>
<p class="p0">&nbsp; &nbsp;懒惰(勉强) 如 "?" &nbsp;懒惰量词使用另一种方式匹配,它从目标的起始位置开始尝试匹配,每次检查一个字符,并寻找它要匹配的内容,如此循环直到字符结尾处。</p>
<p class="p0">&nbsp; &nbsp;占有 &nbsp;如"+" 占有量词会覆盖事个目标字符串,然后尝试寻找匹配内容 ,但它只尝试一次,不会回溯,就好比先抓一把石头,然后从石头中挑出黄金</p>
<p class="p0">&nbsp; &nbsp; <strong>&nbsp;"*"(贪婪)</strong> &nbsp;&nbsp;重复零次或更多</p>
<p class="p0">    &nbsp;例如"aaaaaaaa" 匹配字符串中所有的a &nbsp;正则: "a*" &nbsp; 会出到所有的字符"a"</p>
<p class="p0">&nbsp; &nbsp; &nbsp;<strong>"+"(懒惰)</strong> &nbsp;&nbsp;重复一次或更多次</p>
<p class="p0">   &nbsp; &nbsp; 例如"aaaaaaaa"&nbsp;匹配字符串中所有的a &nbsp;正则: "a+" &nbsp;会取到字符中所有的a字符, &nbsp;"a+"与"a*"不同在于"+"至少是一次而"*" 可以是0次,</p>
<p class="p0">   &nbsp; &nbsp; 稍后会与"?"字符结合来体现这种区别</p>
<p class="p0">&nbsp; &nbsp; &nbsp;<strong>"?"(占有)</strong> &nbsp;&nbsp;重复零次或一次</p>
<p class="p0">   &nbsp; &nbsp; 例如"aaaaaaaa"&nbsp;匹配字符串中的a 正则 :&nbsp;"a?" 只会匹配一次,也就是结果只是单个字符a</p>
<p class="p0">  &nbsp;<strong>"{n}"</strong> &nbsp;重复n次</p>
<p class="p0">   &nbsp; &nbsp; 例如从"aaaaaaaa"&nbsp;匹配字符串的a 并重复3次 正则: &nbsp;"a{3}" &nbsp;结果就是取到3个a字符 &nbsp;"aaa";</p>
<p class="p0">  &nbsp;<strong>"{n,m}"</strong> &nbsp;重复n到m次</p>
<p class="p0">   &nbsp; &nbsp; 例如正则 "a{3,4}" 将a重复匹配3次或者4次 所以供匹配的字符可以是三个"aaa"也可以是四个"aaaa" 正则都可以匹配到</p>
<p class="p0">&nbsp; &nbsp; &nbsp;<strong>"{n,}"</strong> &nbsp;重复n次或更多次</p>
<p class="p0">&nbsp;   &nbsp; &nbsp;与{n,m}不同之处就在于匹配的次数将没有上限,但至少要重复n次 如 正则"a{3,}" a至少要重复3次</p>
<p class="p0">&nbsp;把量词了解了之后之前匹配电话号码的正则现在就可以改得简单点了^0\d\d\d-\d\d\d\d\d\d\d<span class="MathJax_Preview" style="color: inherit;"></span><span tabindex="0" class="MathJax" id="MathJax-Element-1-Frame" role="presentation" style="display: inline-block; position: relative;" data-mathml='<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow class="MJX-TeXAtom-ORD"><mo>&amp;#x53EF;</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>&amp;#x4EE5;</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>&amp;#x6539;</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>&amp;#x4E3A;</mo></mrow><msup><mo>&amp;quot;</mo><mn>0</mn></msup><mtext mathcolor="red">\d</mtext><mo>+</mo><mo>&amp;#x2212;</mo><mtext mathcolor="red">\d</mtext><mrow class="MJX-TeXAtom-ORD"><mn>7</mn></mrow></math>'><nobr aria-hidden="true"><span class="math" id="MathJax-Span-1" role="math" style="width: 11.71em; display: inline-block;"><span style="width: 1px; height: 0px; overflow: hidden; margin-right: -1px; display: inline-block;"></span><span style="width: 9.28em; height: 0px; font-size: 126%; display: inline-block; position: relative;"><span style="left: 0em; top: -4.02em; position: absolute; clip: rect(3.01em, 1009.26em, 4.46em, -1000em);"><span class="mrow" id="MathJax-Span-2"><span class="texatom" id="MathJax-Span-3"><span class="mrow" id="MathJax-Span-4"><span class="mo" id="MathJax-Span-5"><span style="font-family: STIXGeneral,'Arial Unicode MS',serif; font-size: 79%; font-style: normal; font-weight: normal;">可</span></span></span></span><span class="texatom" id="MathJax-Span-6"><span class="mrow" id="MathJax-Span-7"><span class="mo" id="MathJax-Span-8"><span style="font-family: STIXGeneral,'Arial Unicode MS',serif; font-size: 79%; font-style: normal; font-weight: normal;">以</span></span></span></span><span class="texatom" id="MathJax-Span-9"><span class="mrow" id="MathJax-Span-10"><span class="mo" id="MathJax-Span-11"><span style="font-family: STIXGeneral,'Arial Unicode MS',serif; font-size: 79%; font-style: normal; font-weight: normal;">改</span></span></span></span><span class="texatom" id="MathJax-Span-12"><span class="mrow" id="MathJax-Span-13"><span class="mo" id="MathJax-Span-14"><span style="font-family: STIXGeneral,'Arial Unicode MS',serif; font-size: 79%; font-style: normal; font-weight: normal;">为</span></span></span></span><span class="msubsup" id="MathJax-Span-15" style="padding-left: 0.27em;"><span style="width: 1px; height: 0px; overflow: hidden; margin-right: -1px; display: inline-block;"></span><span style="width: 0.92em; height: 0px; display: inline-block; position: relative;"><span style="left: 0em; top: -4.02em; position: absolute; clip: rect(3.15em, 1000.37em, 3.83em, -1000em);"><span class="mo" id="MathJax-Span-16" style="font-family: MathJax_Main-Web;">"</span>&nbsp;<span style="width: 0px; height: 4.02em; display: inline-block;"></span></span><span style="left: 0.5em; top: -4.39em; position: absolute;"><span class="mn" id="MathJax-Span-17" style="font-family: MathJax_Main-Web; font-size: 70.7%;">0</span>&nbsp;<span style="width: 0px; height: 4.02em; display: inline-block;"></span></span></span></span><span class="mtext" id="MathJax-Span-18" style="color: red; padding-left: 0.27em; font-family: MathJax_Main-Web;">\d</span><span class="mo" id="MathJax-Span-19" style="padding-left: 0.22em; font-family: MathJax_Main-Web;">+</span><span class="mo" id="MathJax-Span-20" style="padding-left: 0.22em; font-family: MathJax_Main-Web;">−</span><span class="mtext" id="MathJax-Span-21" style="color: red; font-family: MathJax_Main-Web;">\d</span><span class="texatom" id="MathJax-Span-22"><span class="mrow" id="MathJax-Span-23"><span class="mn" id="MathJax-Span-24" style="font-family: MathJax_Main-Web;">7</span></span></span></span>&nbsp;<span style="width: 0px; height: 4.02em; display: inline-block;"></span></span></span><span style="width: 0px; height: 1.51em; overflow: hidden; vertical-align: -0.39em; border-left-color: currentColor; border-left-width: 0px; border-left-style: solid; display: inline-block;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mrow class="MJX-TeXAtom-ORD"><mo>可</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>以</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>改</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>为</mo></mrow><msup><mo>"</mo><mn>0</mn></msup><mtext mathcolor="red">\d</mtext><mo>+</mo><mo>−</mo><mtext mathcolor="red">\d</mtext><mrow class="MJX-TeXAtom-ORD"><mn>7</mn></mrow></math></span></span><script id="MathJax-Element-1" type="math/tex"> 可以改为"^0\d+-\d{7}</script>"。</p>
<p class="p0">这样写还不够完美如果因为前面的区号没有做限定,以至于可以输入很多们,而通常只能是3位或者4位,</p>
<p class="p0">现在再改一下 "^0\d{2,3}-\d{7}"如此一来区号部分就可以匹配3位或者4位的了</p>
<p class="p0"><span style="font-size: 16px;"><strong>&nbsp;2.4 懒惰限定符</strong></span></p>
<p class="p0">  <strong>"*?"</strong> &nbsp;&nbsp;重复任意次,但尽可能少重复&nbsp;</p>
<p class="p0">   &nbsp; &nbsp;如 "acbacb" &nbsp;正则 &nbsp;"a.*?b" 只会取到第一个"acb" 原本可以全部取到但加了限定符后,只会匹配尽可能少的字符 ,而"acbacb"最少字符的结果就是"acb"&nbsp;</p>
<p class="p0">  <strong>"+?"</strong> &nbsp;重复1次或更多次,但尽可能少重复</p>
<p class="p0">   &nbsp; 与上面一样,只是至少要重复1次</p>
<p class="p0">  <strong>"??"</strong> &nbsp;重复0次或1次,但尽可能少重复</p>
<p class="p0">   &nbsp; &nbsp;如 "aaacb" 正则 "a.??b" 只会取到最后的三个字符"acb"</p>
<p class="p0">  <strong>"{n,m}?"</strong> &nbsp;重复n到m次,但尽可能少重复</p>
<p class="p0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 如 "aaaaaaaa" &nbsp;正则 "a{0,m}" 因为最少是0次所以取到结果为空</p>
<p class="p0">  <strong>"{n,}?" &nbsp;</strong> &nbsp;重复n次以上,但尽可能少重复</p>
<p class="p0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 如 "aaaaaaa" &nbsp;正则 "a{1,}" 最少是1次所以取到结果为 "a"</p>
<p class="p0">&nbsp;</p>
<h3>三 &nbsp;正则进阶</h3>
<p class="p0">&nbsp; &nbsp;<span style="font-size: 16px;"><strong> &nbsp;3.1 捕获分组</strong></span></p>
<p class="p0">  先了解在正则中捕获分组的概念,其实就是一个括号内的内容 如 "(\d)\d" 而"(\d)" 这就是一个捕获分组,可以对捕获分组进行 后向引用 (如果后而有相同的内容则可以直接引用前面定义的捕获组,以简化表达式) 如(\d)\d\1 这里的"\1"就是对"(\d)"的后向引用</p>
<p class="p0">那捕获分组有什么用呢看个例子就知道了</p>
<p class="p0">如 &nbsp;"zery zery" 正则 \b(\w+)\b\s\1\b 所以这里的"\1"所捕获到的字符也是 与(\w+)一样的"zery",为了让组名更有意义,组名是可以自定义名字的</p>
<p class="p0">"\b(?&lt;name&gt;\w+)\b\s\k&lt;name&gt;\b" 用"?&lt;name&gt;"就可以自定义组名了而要后向引用组时要记得写成 "\k&lt;name&gt;";自定义组名后,捕获组中匹配到的值就会保存在定义的组名里</p>
<p class="p0">下面列出捕获分组常有的用法</p>
<p class="p0">&nbsp;</p>
<p class="p0"><span><strong>"(exp)"</strong> &nbsp; &nbsp;<span>匹配exp,并捕获文本到自动命名的组里</span></span></p>
<p class="p0"><span><strong>"(?&lt;name&gt;exp)"</strong> &nbsp;&nbsp;<span>匹配exp,并捕获文本到名称为name的组里</span></span></p>
<p class="p0"><span><strong>"(?:exp)"</strong> &nbsp;<span>匹配exp,不捕获匹配的文本,也不给此分组分配组号</span></span></p>
<p class="p0"><span><span>以下为零宽断言</span></span></p>
<p class="p0"><span><strong>"(?=exp)"</strong> &nbsp;<span>匹配exp前面的位置</span></span></p>
<p class="p0"><span><span>  如 "How are you doing" 正则"(?&lt;txt&gt;.+(?=ing))" 这里取ing前所有的字符,并定义了一个捕获分组名字为 "txt" 而"txt"这个组里的值为"How are you do";</span></span></p>
<p class="p0"><span><strong>"(?&lt;=exp)"</strong> &nbsp;<span>匹配exp后面的位置</span></span></p>
<p class="p0"><span><span>  如 "How are you doing" 正则"(?&lt;txt&gt;(?&lt;=How).+)" 这里取"How"之后所有的字符,并定义了一个捕获分组名字为 "txt" 而"txt"这个组里的值为"&nbsp;are you doing";</span></span></p>
<p class="p0"><span><span><strong>"(?!exp)"</strong> &nbsp;<span>匹配后面跟的不是exp的位置</span></span></span></p>
<p class="p0"><span><span><span>  如 "123abc" 正则 "\d{3}(?!\d)"匹配3位数字后非数字的结果</span></span></span></p>
<p class="p0"><span><span><strong>"(?&lt;!exp)"</strong> &nbsp;<span>匹配前面不是exp的位置</span></span></span></p>
<p class="p0"><span><span><span>  如 "abc123 " 正则 "(?&lt;![0-9])123" 匹配"123"前面是非数字的结果也可写成"(?!&lt;\d)123"</span></span></span></p>
<p class="p0">&nbsp;</p>
<h3>四 &nbsp;正则实战</h3>
<p class="p0">  正则在做验证,与数据过滤时体现的威力是巨大的,我想用过的朋友都知道,下面我们把刚刚了解的全部结合起来做一次实战 做数据采集用正则过滤Html标签并取相应的数据</p>
<p class="p0">我们的战场就选在博客园吧,假设现在要采集博客园首页的所有文章信息 包括文章标题,链接接 作者博客地址,文章简介,文章发布时间,阅读数据,评论数 ,推荐数。</p>
<p class="p0">&nbsp;</p>
<p class="p0">先看博客园文章的Html格式</p>
<div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" onclick="copyCnblogsCode(this)" href="javascript:void(0);"><img alt="复制代码" src="//common.cnblogs.com/images/copycode.gif"></a></span></div>
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" onclick="copyCnblogsCode(this)" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif"></a></span></div>
<pre><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">div </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="post_item"</span><span style="color: rgb(0, 0, 255);">&gt;</span>
<span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">div </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="digg"</span><span style="color: rgb(0, 0, 255);">&gt;</span>
    <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">div </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="diggit"</span><span style="color: rgb(255, 0, 0);"> onclick</span><span style="color: rgb(0, 0, 255);">="DiggIt(3439076,120879,1)"</span><span style="color: rgb(0, 0, 255);">&gt;</span>
    <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">span </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="diggnum"</span><span style="color: rgb(255, 0, 0);"> id</span><span style="color: rgb(0, 0, 255);">="digg_count_3439076"</span><span style="color: rgb(0, 0, 255);">&gt;</span>4<span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">span</span><span style="color: rgb(0, 0, 255);">&gt;</span>
    <span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">div</span><span style="color: rgb(0, 0, 255);">&gt;</span>
    <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">div </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="clear"</span><span style="color: rgb(0, 0, 255);">&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">div</span><span style="color: rgb(0, 0, 255);">&gt;</span>   
    <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">div </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="digg_tip_3439076"</span><span style="color: rgb(255, 0, 0);"> class</span><span style="color: rgb(0, 0, 255);">="digg_tip"</span><span style="color: rgb(0, 0, 255);">&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">div</span><span style="color: rgb(0, 0, 255);">&gt;</span>
<span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">div</span><span style="color: rgb(0, 0, 255);">&gt;</span>     
<span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">div </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="post_item_body"</span><span style="color: rgb(0, 0, 255);">&gt;</span>
    <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">h3</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">a </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="titlelnk"</span><span style="color: rgb(255, 0, 0);"> href</span><span style="color: rgb(0, 0, 255);">="http://www.cnblogs.com/swq6413/p/3439076.html"</span><span style="color: rgb(255, 0, 0);"> target</span><span style="color: rgb(0, 0, 255);">="_blank"</span><span style="color: rgb(0, 0, 255);">&gt;</span>分享完整的项目工程目录结构<span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(0, 0, 255);">&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">h3</span><span style="color: rgb(0, 0, 255);">&gt;</span>                  
    <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">p </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="post_item_summary"</span><span style="color: rgb(0, 0, 255);">&gt;</span>
<span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">a </span><span style="color: rgb(255, 0, 0);">href</span><span style="color: rgb(0, 0, 255);">="http://www.cnblogs.com/swq6413/"</span><span style="color: rgb(255, 0, 0);"> target</span><span style="color: rgb(0, 0, 255);">="_blank"</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">img </span><span style="color: rgb(255, 0, 0);">width</span><span style="color: rgb(0, 0, 255);">="48"</span><span style="color: rgb(255, 0, 0);"> height</span><span style="color: rgb(0, 0, 255);">="48"</span><span style="color: rgb(255, 0, 0);"> class</span><span style="color: rgb(0, 0, 255);">="pfs"</span><span style="color: rgb(255, 0, 0);"> src</span><span style="color: rgb(0, 0, 255);">="http://pic.cnitblog.com/face/142964/20131116170946.png"</span><span style="color: rgb(255, 0, 0);"> alt</span><span style="color: rgb(0, 0, 255);">=""</span><span style="color: rgb(0, 0, 255);">/&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">    在项目开发过程中,如何有序的保存项目中的各类数据文件,建立一个分类清晰、方便管理的目录结构是非常重要的。 综合以前的项目和一些朋友的项目结构,我整理了一份我觉得还不错的项目目录结构。 在这里分享给大家,欢迎各位提出你宝贵的意见和建议。如果喜欢请“推荐”则个,感激万分!! 整个目录设置到4级子目录,实...
    </span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">p</span><span style="color: rgb(0, 0, 255);">&gt;</span>             
    <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">div </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="post_item_foot"</span><span style="color: rgb(0, 0, 255);">&gt;</span>                   
    <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">a </span><span style="color: rgb(255, 0, 0);">href</span><span style="color: rgb(0, 0, 255);">="http://www.cnblogs.com/swq6413/"</span><span style="color: rgb(255, 0, 0);"> class</span><span style="color: rgb(0, 0, 255);">="lightblue"</span><span style="color: rgb(0, 0, 255);">&gt;</span>七少爷<span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">
    发布于 2013-11-23 15:48
    </span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">span </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="article_comment"</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">a </span><span style="color: rgb(255, 0, 0);">href</span><span style="color: rgb(0, 0, 255);">="http://www.cnblogs.com/swq6413/p/3439076.html#commentform"</span><span style="color: rgb(255, 0, 0);"> title</span><span style="color: rgb(0, 0, 255);">="2013-11-23 16:40"</span><span style="color: rgb(255, 0, 0);"> class</span><span style="color: rgb(0, 0, 255);">="gray"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">
        评论(4)</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(0, 0, 255);">&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">span</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">span </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="article_view"</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">a </span><span style="color: rgb(255, 0, 0);">href</span><span style="color: rgb(0, 0, 255);">="http://www.cnblogs.com/swq6413/p/3439076.html"</span><span style="color: rgb(255, 0, 0);"> class</span><span style="color: rgb(0, 0, 255);">="gray"</span><span style="color: rgb(0, 0, 255);">&gt;</span>阅读(206)<span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(0, 0, 255);">&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">span</span><span style="color: rgb(0, 0, 255);">&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">div</span><span style="color: rgb(0, 0, 255);">&gt;</span>
<span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">div</span><span style="color: rgb(0, 0, 255);">&gt;</span>
<span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">div </span><span style="color: rgb(255, 0, 0);">class</span><span style="color: rgb(0, 0, 255);">="clear"</span><span style="color: rgb(0, 0, 255);">&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">div</span><span style="color: rgb(0, 0, 255);">&gt;</span>
<span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">div</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre>
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" onclick="copyCnblogsCode(this)" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif"></a></span></div>
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" onclick="copyCnblogsCode(this)" href="javascript:void(0);"><img alt="复制代码" src="//common.cnblogs.com/images/copycode.gif"></a></span></div></div>
<p>&nbsp;</p>
<p class="p0">&nbsp;</p>
<p class="p0">&nbsp;通过构造一个Http请求来取到数据并对数据进行相应处理得到关键信息,在过滤Html标签取文章时正则的强大的威力就体现出来了,</p>
<p class="p0">正则的知识点也都基本用上了比如 "\s \w+ . * ? "还有捕获分组,零宽断言等等。喜欢的朋友可以试一试,然后自己看如何通过正则取相应数据的,代码中的正则都是很基本简单的,其意思与用法都在上文中详细写了。</p>
<p class="p0">&nbsp;</p>
<div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" onclick="copyCnblogsCode(this)" href="javascript:void(0);"><img alt="复制代码" src="//common.cnblogs.com/images/copycode.gif"></a></span></div>
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" onclick="copyCnblogsCode(this)" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif"></a></span></div>
<pre>    <span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Program
    {
        </span><span style="color: rgb(0, 0, 255);">static</span> <span style="color: rgb(0, 0, 255);">void</span> Main(<span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">[] args)
        {
        
            </span><span style="color: rgb(0, 0, 255);">string</span> content =<span style="color: rgb(0, 0, 0);"> HttpUtility.HttpGetHtml();
            HttpUtility.GetArticles(content);
        }
    }

    </span><span style="color: rgb(0, 0, 255);">internal</span> <span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> HttpUtility
    {
        </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">默认获取第一页数据</span>
        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">static</span> <span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> HttpGetHtml()
        {
            HttpWebRequest request </span>= (HttpWebRequest)WebRequest.Create(<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">http://www.cnblogs.com/</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
            request.Accept </span>= <span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">text/plain, */*; q=0.01</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;
            request.Method </span>= <span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">GET</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;
            request.Headers.Add(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Accept-Language</span><span style="color: rgb(128, 0, 0);">"</span>, <span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);
            request.ContentLength </span>= <span style="color: rgb(128, 0, 128);">0</span><span style="color: rgb(0, 0, 0);">;
           </span><span style="color: rgb(0, 0, 0);">
            request.Host </span>= <span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">www.cnblogs.com</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;
            request.UserAgent </span>= <span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Maxthon/4.1.3.5000 Chrome/26.0.1410.43 Safari/537.1</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;
            HttpWebResponse response </span>=<span style="color: rgb(0, 0, 0);"> (HttpWebResponse)request.GetResponse();
            Stream responStream </span>=<span style="color: rgb(0, 0, 0);"> response.GetResponseStream();
            StreamReader reader </span>= <span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> StreamReader(responStream, Encoding.UTF8);
            </span><span style="color: rgb(0, 0, 255);">string</span> content =<span style="color: rgb(0, 0, 0);"> reader.ReadToEnd();
            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> content;

        }

        </span><span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">static</span> List&lt;Article&gt; GetArticles(<span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> htmlString)
        {
            List</span>&lt;Article&gt; articleList = <span style="color: rgb(0, 0, 255);">new</span> List&lt;Article&gt;<span style="color: rgb(0, 0, 0);">();
            Regex regex </span>= <span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">;
            Article article </span>= <span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">;
            regex </span>= <span style="color: rgb(0, 0, 255);">new</span> Regex(<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">&lt;div class=\"post_item\"&gt;(?&lt;content&gt;.*?)(?=&lt;div class=\"clear\"&gt;</span><span style="color: rgb(128, 0, 0);">"</span> + <span style="color: rgb(128, 0, 0);">@"</span><span style="color: rgb(128, 0, 0);">&lt;/div&gt;\s*&lt;/div&gt;)</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,
                              RegexOptions.Singleline);

            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (regex.IsMatch(htmlString))
            {
                MatchCollection aritcles </span>=<span style="color: rgb(0, 0, 0);"> regex.Matches(htmlString);

                </span><span style="color: rgb(0, 0, 255);">foreach</span> (Match item <span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> aritcles)
                {
                    article </span>= <span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Article();
                    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">取推荐</span>
                    regex =
                        <span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Regex(
                            </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">&lt;div class=\"digg\"&gt;.*&lt;span.*&gt;(?&lt;digNum&gt;.*)</span><span style="color: rgb(128, 0, 0);">"</span> + <span style="color: rgb(128, 0, 0);">@"</span><span style="color: rgb(128, 0, 0);">&lt;/span&gt;</span><span style="color: rgb(128, 0, 0);">"</span> +
                            <span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">.*&lt;div class=\"post_item_body\"&gt;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, RegexOptions.Singleline);
                    article.DiggNum </span>= regex.Match(item.Value).Groups[<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">digNum</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].Value;

                    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">取文章标题 需要去除转义字符</span>
                    regex = <span style="color: rgb(0, 0, 255);">new</span> Regex(<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">&lt;h3&gt;(?&lt;a&gt;.*)&lt;/h3&gt;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, RegexOptions.Singleline);
                    </span><span style="color: rgb(0, 0, 255);">string</span> a = regex.Match(item.Value).Groups[<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].Value;
                    regex </span>= <span style="color: rgb(0, 0, 255);">new</span> Regex(<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">&lt;a\\s.*href=\"(?&lt;href&gt;.*?)\".*&gt;(?&lt;summary&gt;.*)&lt;/a&gt;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, RegexOptions.Singleline);
                    article.AritcleUrl </span>= regex.Match(a).Groups[<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">href</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].Value;
                    article.AritcleTitle </span>= regex.Match(a).Groups[<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">summary</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].Value;

                    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">取作者图片 </span>
                    regex = <span style="color: rgb(0, 0, 255);">new</span> Regex(<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">&lt;a.*&gt;(?&lt;img&gt;&lt;img[^&gt;].*&gt;)&lt;/a&gt;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, RegexOptions.Singleline);
                    article.AuthorImg </span>= regex.Match(item.Value).Groups[<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">img</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].Value;

                    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">取作者博客URL及链接的target属性</span>
                    regex = <span style="color: rgb(0, 0, 255);">new</span> Regex(<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">&lt;a\\s*?href=\"(?&lt;href&gt;.*)\"\\s*?target=\"(?&lt;target&gt;.*?)\"&gt;.*&lt;/a&gt;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,
                                      RegexOptions.Singleline);
                    article.AuthorUrl </span>= regex.Match(item.Value).Groups[<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">href</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].Value;
                    </span><span style="color: rgb(0, 0, 255);">string</span> urlTarget = regex.Match(item.Value).Groups[<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">target</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].Value;

                    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">取文章简介
                    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">1 先取summary Div中所有内容</span>
                    regex = <span style="color: rgb(0, 0, 255);">new</span> Regex(<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">&lt;p class=\"post_item_summary\"&gt;(?&lt;summary&gt;.*)&lt;/p&gt;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, RegexOptions.Singleline);
                    </span><span style="color: rgb(0, 0, 255);">string</span> summary = regex.Match(item.Value).Groups[<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">summary</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].Value;
                    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">2 取简介</span>
                    regex = <span style="color: rgb(0, 0, 255);">new</span> Regex(<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">(?&lt;indroduct&gt;(?&lt;=&lt;/a&gt;).*)</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, RegexOptions.Singleline);
                    article.AritcleInto </span>= regex.Match(summary).Groups[<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">indroduct</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].Value;


                    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">取发布人与发布时间</span>
                    regex =
                        <span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Regex(
                            </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">&lt;div class=\"post_item_foot\"&gt;\\s*&lt;a.*?&gt;(?&lt;publishName&gt;.*)&lt;/a&gt;(?&lt;publishTime&gt;.*)&lt;span class=\"article_comment\"&gt;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,
                            RegexOptions.Singleline);
                    article.Author </span>= regex.Match(item.Value).Groups[<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">publishName</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].Value;
                    article.PublishTime </span>= regex.Match(item.Value).Groups[<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">publishTime</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].Value.Trim();

                    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">取评论数</span>
                    regex =
                        <span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Regex(
                            </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">&lt;span class=\"article_comment\"&gt;&lt;a.*&gt;(?&lt;comment&gt;.*)&lt;/a&gt;&lt;/span&gt;&lt;span class=\"article_view\"&gt;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,
                            RegexOptions.Singleline);
                    article.CommentNum </span>= regex.Match(item.Value).Groups[<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">comment</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].Value;

                    </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">取阅读数</span>
                    regex = <span style="color: rgb(0, 0, 255);">new</span> Regex(<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">&lt;span\\s*class=\"article_view\"&gt;&lt;a.*&gt;(?&lt;readNum&gt;.*)&lt;/a&gt;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, RegexOptions.Singleline);
                    article.ReadNum </span>= regex.Match(item.Value).Groups[<span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">readNum</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">].Value;
                    articleList.Add(article);
                }

            }
            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> articleList;
        }


        </span><span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">static</span> <span style="color: rgb(0, 0, 255);">string</span> ClearSpecialTag(<span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> htmlString)
        {

            </span><span style="color: rgb(0, 0, 255);">string</span> htmlStr = Regex.Replace(htmlString, <span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">\n</span><span style="color: rgb(128, 0, 0);">"</span>, <span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">, RegexOptions.IgnoreCase);
            htmlStr </span>= Regex.Replace(htmlStr, <span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">\t</span><span style="color: rgb(128, 0, 0);">"</span>, <span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">, RegexOptions.IgnoreCase);
            htmlStr </span>= Regex.Replace(htmlStr, <span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">\r</span><span style="color: rgb(128, 0, 0);">"</span>, <span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">, RegexOptions.IgnoreCase);
            htmlStr </span>= Regex.Replace(htmlStr, <span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">\"</span><span style="color: rgb(128, 0, 0);">"</span>, <span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, RegexOptions.IgnoreCase);
            </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> htmlStr;
        }
    }

    </span><span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Article
    {
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;summary&gt;</span>
        <span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> 文章标题
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;/summary&gt;</span>
        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">string</span> AritcleTitle { <span style="color: rgb(0, 0, 255);">get</span>; <span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);">; }
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;summary&gt;</span>
        <span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> 文章链接
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;/summary&gt;</span>
        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">string</span> AritcleUrl { <span style="color: rgb(0, 0, 255);">get</span>; <span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);">; }
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;summary&gt;</span>
        <span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> 文章简介
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;/summary&gt;</span>
        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">string</span> AritcleInto { <span style="color: rgb(0, 0, 255);">get</span>; <span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);">; }
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;summary&gt;</span>
        <span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> 作者名
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;/summary&gt;</span>
        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">string</span> Author { <span style="color: rgb(0, 0, 255);">get</span>; <span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);">; }
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;summary&gt;</span>
        <span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> 作者地址
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;/summary&gt;</span>
        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">string</span> AuthorUrl { <span style="color: rgb(0, 0, 255);">get</span>; <span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);">; }
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;summary&gt;</span>
        <span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> 作者图片
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;/summary&gt;</span>
        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">string</span> AuthorImg { <span style="color: rgb(0, 0, 255);">get</span>; <span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);">; }
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;summary&gt;</span>
        <span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> 发布时间
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;/summary&gt;</span>
        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">string</span> PublishTime { <span style="color: rgb(0, 0, 255);">get</span>; <span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);">; }
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;summary&gt;</span>
        <span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> 推荐数
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;/summary&gt;</span>
        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">string</span> DiggNum { <span style="color: rgb(0, 0, 255);">get</span>; <span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);">; }

        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;summary&gt;</span>
        <span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> 评论数
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;/summary&gt;</span>
        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">string</span> CommentNum { <span style="color: rgb(0, 0, 255);">get</span>; <span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);">; }
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;summary&gt;</span>
        <span style="color: rgb(128, 128, 128);">///</span><span style="color: rgb(0, 128, 0);"> 阅读数
        </span><span style="color: rgb(128, 128, 128);">///</span> <span style="color: rgb(128, 128, 128);">&lt;/summary&gt;</span>
        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">string</span> ReadNum { <span style="color: rgb(0, 0, 255);">get</span>; <span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);">; }

    }</span></pre>
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" onclick="copyCnblogsCode(this)" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif"></a></span></div>
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" onclick="copyCnblogsCode(this)" href="javascript:void(0);"><img alt="复制代码" src="//common.cnblogs.com/images/copycode.gif"></a></span></div></div>
<p>&nbsp;正则部分可能写得不很完美,但至少也匹配出来了,另外因为自己也是刚接触正则,也只能写出这种比较简单的正则。还望大家海涵~~</p>
<p class="p0">&nbsp;</p>
<p class="p0">&nbsp;</p>
<h3>五 &nbsp; &nbsp;总结</h3>
<p class="p0">  正则其实并不难,了解每个符号的意思后,自己马上动手试一试多写几次自然就明白了,正则是出了名的坑多,随便少写了个点就匹配不到数据了,我也踩了很多坑,踩着踩着就踩出经验了。</p>
<p class="p0">本文也只是对正则做了很基本的介绍,还有很多正则的字符没有介绍,只是写了比较常用的一些。如有错误之处,还望在评论中指出,我会马上修改。</p></div><div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory">分类: <a href="http://www.cnblogs.com/China3S/category/378034.html" target="_blank">编程语言</a>,<a href="http://www.cnblogs.com/China3S/category/513464.html" target="_blank">正则表达式</a></div>
<div id="EntryTag"></div>
<div id="blog_post_info"><div id="green_channel">
        <a id="green_channel_digg" onclick="DiggIt(3451971,cb_blogId,1);green_channel_success(this,'谢谢推荐!');" href="javascript:void(0);">好文要顶</a>
            <a id="green_channel_follow" onclick="follow('398559f5-e070-e111-aa3f-842b2b196315');" href="javascript:void(0);">关注我</a>
    <a id="green_channel_favorite" onclick="AddToWz(cb_entryId);return false;" href="javascript:void(0);">收藏该文</a>
    <a title="分享至新浪微博" id="green_channel_weibo" onclick="ShareToTsina()" href="javascript:void(0);"><img alt="" src="//common.cnblogs.com/images/icon_weibo_24.png"></a>
    <a title="分享至微信" id="green_channel_wechat" onclick="shareOnWechat()" href="javascript:void(0);"><img alt="" src="//common.cnblogs.com/images/wechat.png"></a>
</div>
<div id="author_profile">
    <div class="author_profile_info" id="author_profile_info">
            <a href="http://home.cnblogs.com/u/China3S/" target="_blank"><img class="author_avatar" alt="" src="//pic.cnblogs.com/face/u388183.gif?id=06153551"></a>
        <div class="author_profile_info" id="author_profile_detail">
            <a href="http://home.cnblogs.com/u/China3S/">向北方</a><br>
            <a href="http://home.cnblogs.com/u/China3S/followees">关注 - 12</a><br>
            <a href="http://home.cnblogs.com/u/China3S/followers">粉丝 - 62</a>
        </div>
    </div>
    <div class="clear"></div>
    <div id="author_profile_honor"></div>
    <div id="author_profile_follow">
                <a onclick="follow('398559f5-e070-e111-aa3f-842b2b196315');return false;" href="javascript:void(0);">+加关注</a>
    </div>
</div>
<div id="div_digg">
    <div class="diggit" onclick="votePost(3451971,'Digg')">
        <span class="diggnum" id="digg_count">8</span>
    </div>
    <div class="buryit" onclick="votePost(3451971,'Bury')">
        <span class="burynum" id="bury_count">0</span>
    </div>
    <div class="clear"></div>
    <div class="diggword" id="digg_tips">
    </div>
</div>
</div>
<div class="clear"></div>
<div id="post_next_prev"><a class="p_n_p_prefix" href="http://www.cnblogs.com/China3S/p/3447572.html">« </a> 上一篇:<a title="发布于2013-11-28 13:58" href="http://www.cnblogs.com/China3S/p/3447572.html">如何解决SWAT模型数据移动目录后出现的“SWAT2005.mdb database specified in your MasterProgress table does not exists. Please correct and try again”的问题</a><br><a class="p_n_p_prefix" href="http://www.cnblogs.com/China3S/p/3451983.html">» </a> 下一篇:<a title="发布于2013-11-30 23:57" href="http://www.cnblogs.com/China3S/p/3451983.html">Hashtable、Dictionary和List 谁效率更高</a><br></div>
</div>


 </div>

0 0
原创粉丝点击