<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>我爱正则表达式</title>
	<atom:link href="http://iregex.org/feed" rel="self" type="application/rss+xml" />
	<link>http://iregex.org</link>
	<description>关注正则表达式和搜索引擎</description>
	<pubDate>Fri, 07 Nov 2008 08:11:42 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
	<language>en</language>
			<item>
		<title>[译]从文本中析取有效URL链接</title>
		<link>http://iregex.org/blog/translate-detecting-urls-in-a-block-of-text.html</link>
		<comments>http://iregex.org/blog/translate-detecting-urls-in-a-block-of-text.html#comments</comments>
		<pubDate>Fri, 07 Nov 2008 08:11:42 +0000</pubDate>
		<dc:creator>rex</dc:creator>
		
		<category><![CDATA[resources]]></category>

		<category><![CDATA[regexbuddy]]></category>

		<category><![CDATA[translation]]></category>

		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=36</guid>
		<description><![CDATA[原文作者是Jan Goyvaerts(Regex Guru)，原页面链接是Detecting URLs in a Block of Text， 
翻译者：rex，译者博客(http://iregex.org)。
rex注：URL是Uniform Resource Locator的缩写(wiki)，中文叫作统一资源定位符（百科），解释如下：Internet上的每一个网页都具有一个唯一的名称标识，通常称之为URL地址，这种地址可以是本地磁盘，也可以是局域网上的某一台计算机，更多的是Internet上的站点。简单地说，URL就是Web地址，俗称&#8220;网址&#8221;。
In his blog post The Problem with URLs points out some of the issues with trying to detect URLs in a larger body of text using a regular expression. 
在他的博客文章URL难题一文中指出了使用正则表达式在大量文本中尝试检测URL所遇到的一些问题。
The short answer is that it can&#8217;t be done. Pretty much any character is valid [...]]]></description>
			<content:encoded><![CDATA[<h5>原文作者是Jan Goyvaerts(<a href="http://www.regex-guru.info">Regex Guru</a>)，原页面链接是<a href="http://www.regex-guru.info/2008/11/detecting-urls-in-a-block-of-text/">Detecting URLs in a Block of Text</a>， </p>
<p>翻译者：rex，译者博客(<a href="http://iregex.org">http://iregex.org</a>)。</h5>
<p>rex注：URL是Uniform Resource Locator的缩写(<a target="_blank" href="http://en.wikipedia.org/wiki/URL">wiki</a>)，中文叫作<strong>统一资源定位符</strong>（<a target="_blank" href="http://baike.baidu.com/view/1496.htm">百科</a>），解释如下：Internet上的每一个网页都具有一个唯一的名称标识，通常称之为URL地址，这种地址可以是本地磁盘，也可以是局域网上的某一台计算机，更多的是Internet上的站点。简单地说，URL就是Web地址，俗称&#8220;网址&#8221;。</p>
<p>In his blog post <a href="http://www.codinghorror.com/blog/archives/001181.html">The Problem with URLs</a> points out some of the issues with trying to detect URLs in a larger body of text using a regular expression. </p>
<p>在他的博客文章<a target="_blank" href="http://www.codinghorror.com/blog/archives/001181.html">URL难题</a>一文中指出了使用正则表达式在大量文本中尝试检测URL所遇到的一些问题。</p>
<p>The short answer is that it <b>can&#8217;t be done</b>. Pretty much <b>any character is valid in URLs</b>. The very simplistic \bhttp://\S+ not only fails to differentiate between punctuation that&#8217;s part of the URL, and punctuation used to quote the URL. It also fails to match URLs with spaces in them. Yes, spaces are valid in URLs, and I&#8217;ve encountered quite a few web sites that use them over the years. It also forgets other protocols, such as https. </p>
<p>简言之，答案是<strong>做不到</strong>。在多数情况下，<strong>任何字符在URL中都是合法字符</strong>。这条过分简单化的表达式<tt class="regex">\bhttp://\S+</tt>之所以失败，不单因为它无法区分作为URL一部分的标点符号与引用URL的标点符号，还在于它对URL中包含空格的情况也是无能为力。是的，空格在URL中也是合法的，这几年我颇遇到一些网址中包含空格的情况。本条正则式还忽略了其它的网络协议，例如https。</p>
<p>In <a href="http://www.regexbuddy.com/library.html">RegexBuddy&#8217;s library</a>, you&#8217;ll find this regex if you look up &#8220;URL: Find in full text&#8221;: </p>
<p>在<a href="http://www.regexbuddy.com/library.html">RegexBuddy&#8217;s library</a>(RegexBuddy标准库)中，你如果你查找&#8220;URL: Find in full text&#8221;：</p>
<p><tt class="regex">\b(https?|ftp|file)://[-A-Z0-9+&amp;@#/%?=~_|!:,.;]*[A-Z0-9+&amp;@#/%=~_|]</tt> (case insensitive大小写不敏感)</p>
<p>Like every other regex for extracting URLs, it&#8217;s <b>not perfect</b>. The key benefit of this regex is that it uses a separate character class for the last character in the URL, which allows less punctuation characters than the character class for the other characters in the URL. It excludes punctuation that is unlikely to occur at the end of the URL, and more likely to be punctuation that&#8217;s part of the sentence the URL is quoted in. It does not allow parentheses at all. </p>
<p>与其它试图析取URL的正则式一样，它<strong>并不完美</strong>。但是这条正则式的主要优势是，它在结尾处使用单独的文本类，这就限定了结尾处所允许出现的标点字符种类要少于URL的其它部分。它排除了在URL结尾处不太可能出现，而更像是URL所在文本结尾的标点。它根本不允许出现括号。</p>
<p>In <a href="http://www.editpadpro.com/cscs.html">EditPad Pro&#8217;s syntax coloring schemes</a>, which are fully editable and entirely based on regular expressions, you&#8217;ll often find this regex: </p>
<p><a target="_blank" href="http://www.editpadpro.com/cscs.html">EditPad Pro的语法色彩主题</a>是可自定义的，完全基于正则表达式的。在其中，你会经常发现这条正则式：</p>
<p><tt class="regex">\b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&amp;@#/%=~_|$?!:,.]*[A-Z0-9+&amp;@#/%=~_|$]</tt> </p>
<p>(case insensitive大小写不敏感)</p>
<p>The main difference with the previous regex is that this one matches URLs such as www.regex-guru.info <b>without the http:// protocol</b>. People often type URLs that way in their documents and messages, because most browsers accept them that way too. </p>
<p>本条正则式与上一条的主要区别是，它匹配<tt class="string">www.regex-guru.info</tt>之类的URL，之前没有<tt class="string">http://</tt>协议。人们经常在文件或消息中使用这种方式输入网址，同时大多数浏览器也接受这种方式。</p>
<p>EditPad&#8217;s built-in &#8220;clickable URLs&#8221; syntax highlighting uses this regex:<br />
  <br />EditPad的内置&#8220;可点击的URL&#8221;语法高亮，是由这条正则式实现的：</p>
<p><tt class="regex">\b(?:(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&amp;@#/%?=~_|$!:,.;]*[-A-Z0-9+&amp;@#/%=~_|$]</tt> </p>
<p><tt class="regex">&#160;&#160; | ((?:mailto:)?[A-Z0-9._%+-]+@[A-Z0-9._%-]+\.[A-Z]{2,4})\b)</tt> </p>
<p><tt class="regex">|&#8221;(?:(?:https?|ftp|file)://|www\.|ftp\.)[^&quot;\r\n]+&#8221;?</tt> </p>
<p><tt class="regex">|&#8217;(?:(?:https?|ftp|file)://|www\.|ftp\.)[^'\r\n]+&#8217;?</tt> (free-spacing, case insensitive空格宽松模式，大小写不敏感)</p>
<p>This log regex adds three alternatives to the previous regex. It adds the ability to match <b>email addresses</b>, with or without mailto:, and it matches <b>URLs between single or double quotes</b>. When the URL is quoted, it allows all characters in the URL, except line breaks and the delimiting quote. This way, any URL with weird punctuation can be highlighted correctly by placing it between a pair of quote characters. Because this regex is used to highlight text as you type, the closing quotes are optional. The highlighting will run until the end of the line until you type the closing quote. Remove the question marks after the quote characters if you will use this regex to extract URLs. </p>
<p>这条正则式在前一条的基础上增加了三种备选匹配项。现在它可以匹配电邮地址，有无mailto:均可；匹配<strong>单引号或双引号之间的URL</strong>。当URL在引号内时，它允许出现除换行符或起界作用的引号之外的任意字符。使用这种方法，不论使用何种怪异标点引用的URL，都能将URL与引号分离开来，从而正确高亮显示。由于此正则式是在你输入的同时高亮文本，因此右侧的结尾引号不是必需的。高亮持续显示到本行结尾，直到你输入右侧的结尾引号。如果你要使用本正则式析取URL，请删除引号后面的引号。</p>
<p>So how about Jeff&#8217;s problem?<br />
  <br />我们再来看一下Jeff的问题：</p>
<blockquote>
<p>I couldn&#8217;t come up with a way for the regex alone to distinguish between URLs that legitimately end in parens (ala Wikipedia), and URLs that the user has enclosed in parens.<br />
    <br />我实在想不出，怎样仅仅使用正则式，就能正确地区分使用以括号结尾的URL和使用括号括起来的URL。</p>
</blockquote>
<p>That&#8217;s not too hard, if we add the restriction that we only allow unnested pairs of parentheses in URLs. Using the second regex in this article as the starting point, <b>add an alternative for a pair of parentheses to both character classes</b> in that regex: </p>
<p>如果限制在URL只使用不嵌套的括号的话，那么该问题不难解决。使用本文中第二条正则式作为开始，<strong>在正则式的两组字符类中都加上一对可选的括号</strong>：</p>
<p><tt class="regex">\b(?:(?:https?|ftp|file)://|www\.|ftp\.) </tt><br />
    <br /><tt class="regex">&#160; (?:\([-A-Z0-9+&amp;@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&amp;@#/%=~_|$?!:,.])* </tt></p>
<p><tt class="regex">&#160; (?:\([-A-Z0-9+&amp;@#/%=~_|$?!:,.]*\)|[A-Z0-9+&amp;@#/%=~_|$])</tt> (free-spacing, case insensitive空格宽松模式，大小写不敏感)</tt></p>
<p>This regex allows the same set of characters in the middle of the URL, mixed with zero or more sequences of those characters between parentheses. It allows the URL to end with the same reduced set of characters, or a final run between parentheses. Because we require the opening parenthesis to be in the URL, we don&#8217;t have to do anything complicated to check if any closing parentheses we encounter are part of the URL or not.<br />
  <br />这条正则式允许相同的字符集出现在URL中的括号内，组成0个或多个字符的序列。它允许URL以简化的字符集结尾，或者在结尾出现最后一组括号。由于我们要求在URL中出现开括号，所以就不必再做任何复杂工作来验证之后所遇到的闭括号是不是URL的一部分了。</p>
<p>It&#8217;s important that you observe that in order to allow any number of pairs of parentheses in the middle of the regex, I <b>moved the star</b> from the character class to the group it is now in. I did <b>not add another star</b> to the group. A double-star combination like <tt class="regex">(a|b*)*</tt> is a sure-fire recipe for <a href="http://www.regular-expressions.info/catastrophic.html">catastrophic backtracking</a>. </p>
<p>你或许注意到，为了允许正则式中出现任意多次的成对括号，我将字符类中的<strong>星号移到它所在组</strong>。我并没有再给此组加星号。这一点至关重要。像<tt class="regex">(a|b*)*</tt>这样的双星号组合，是<a target="_blank" href="http://www.regular-expressions.info/catastrophic.html">灾难式回溯</a>的保证。 </p>
<p></p>
<p>All the regexes in this article will be included in RegexBuddy&#8217;s library with the next free minor update. Current version is 3.2.0.<br />
  <br />本文提及的所有的正则式都将在RegexBuddy下次免费次要升级中添加到正则库库中。当前RegexBuddy的版本是3.2.0。</p>
<p>rex注：在RegexBuddy里，可以使用Alt+7唤出自带的正则库。输入URL回车，查到的结果如下：</p>
<p>&#160; <br /><a title="我爱正则达式" target="_blank" href="http://iregex.org"><img style="border-bottom: rgb(255,255,255) 1px solid; border-left: rgb(255,255,255) 1px solid; margin: 0px 10px 10px; padding-left: 0px; clear: both; border-top: rgb(255,255,255) 1px solid; border-right: rgb(255,255,255) 1px solid" src="http://i37.tinypic.com/20sj0j7.jpg" /></a> </p>
<h3>译后：</h3>
<p>我自己探索时，走得很兴奋；有人引路时，很走得很稳健。</p>
<p>抓饭最初就是使用正则表达式析取饭否消息的， 但是所有的文本都遵循良好的格式：XML。当然不可避免非法字符，这些在作为XML展示时会报错。不过，如果镶嵌在HTML中展示时安然无恙。</p>
<p>饭否、叽歪、做啥，都能完美地解析所输入的消息中的URL，并自做主张处理一下，变成自有格式，例如，饭否会把输入的<a href="http://regex.me">http://regex.me</a> 转换为<a title="http://fanfou.com/linkto/aHR0cDovL3JlZ2V4Lm1l" href="http://fanfou.com/linkto/aHR0cDovL3JlZ2V4Lm1l">http://fanfou.com/linkto/aHR0cDovL3JlZ2V4Lm1l</a>，即使在<a href="http://regex.me">http://regex.me</a>之后加上一两个汉字，也不影响转换结果。不过twitter就没这么强大了。在twitter中输入&#8220;<a href="http://regex.me">http://regex.me</a>正则表达式交流论坛&#8221;，输入的的结果是<a href="http://tinyurl.com/5azr6y">http://tinyurl.com/5azr6y</a>，点击展开，URL就成了<a title="http://www.regex.xn--me-y82c39klqi9nmf5umndl14f76g8ol/" href="http://www.regex.me">http://www.regex.me正则表达式交流论坛/</a>，很显然这是一个错误的URL。错误的原因就在于它没有正确地从文本解析URL。</p>
<p><a target="_blank" href="http://fanfou.com/statuses/xWerhzyROOU">撕烤者</a>说过：&#8220;汉语的一个优势是，我们可以很轻易的把它们和代码区分开。&#8221;颇然其说。中英文的区别，不仅仅是肉眼上一目了然，即使使用程序来区分也是毫不费力。不同的编码方案有利于正确解析HTML代码，找到其中的URL结束点。可惜twitter这一点做得实在太滥。</p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/translate-detecting-urls-in-a-block-of-text.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>饭否消息解析之从minidom到xpath</title>
		<link>http://iregex.org/blog/fanfou-message-extractor-from-minidom-to-xpath.html</link>
		<comments>http://iregex.org/blog/fanfou-message-extractor-from-minidom-to-xpath.html#comments</comments>
		<pubDate>Tue, 14 Oct 2008 10:00:58 +0000</pubDate>
		<dc:creator>rex</dc:creator>
		
		<category><![CDATA[tutorial]]></category>

		<category><![CDATA[fanfou]]></category>

		<category><![CDATA[firefox]]></category>

		<category><![CDATA[python]]></category>

		<category><![CDATA[regex]]></category>

		<category><![CDATA[xml]]></category>

		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=35</guid>
		<description><![CDATA[抛板砖，引白玉：为何不用xpath，什么是xpath？
最近拾起了以前的小项目，在完善上篇文章发布后，“那个谁”的回复让我很感兴趣。他问，“为什么不用xpath？”
xpath是什么东东？我反问。反问之前，当然少不了先google一番，以免……那个啥。
首先映入眼帘的是w3c ，对xpath的介绍如下：  
XPath is a language for addressing parts of an XML document, designed to be used by both XSLT and XPointer. 
直译为中文就是，
XPath 是一种语言，用于在XML文档中定位各部分内容，可由XSLT或XPointer调用。
还搜索到xpath的教程，在这里。草草看过，当时并未着意。
虽如此，但是python里的minidom模块，也有此功效呀。为什么非要使用xpath呢？尤其是考虑到在python中还需要额外安装，不如minidom之放之四海而皆可运行。
跟那个谁再交流，意见仍是“力荐”。还推荐我细读教程，并在firefox里使用XPath Checker插件。
于是就照办了。
发硎新试,其快可知
一试XPath Checker，果然石破天惊。选中部分网页文字后，在右键菜单中选&#8221;View Xpath&#8221;，立即显示出该节点的XPath路径。层次清晰，定位精准。只是我对其语法尚未了了。于是细读教程，边学边用；半小时后，已经能够运用到之前写的饭否信息抓取程序上。虽然写代码还有些吃力，但是思路很清晰，不会纠缠于细节中无法脱身。
那个谁还提议，一般的html文档不是标准的xml文档，因此用xpath解析时，最好格式化一下。
我也注意到这个问题了。从饭否html中取出的有用内容，只占全文的一小部分；额外的部分白白拖慢速度，增强析取难度。
经过实验，我将原代码改进如下：
1. 仍用原来的minidom模块下载、分析文档，只取&#60;ol&#62;与&#60;/ol&#62;之间的部分。这部分保存成字符串格式，备用。只取需要的那部分，使结构清晰，层次浅显。
2. 使用xpath来解析上一步取出的字串。
到现在，/，//，@，[]，=，等等，每个符号都从原来的meaningless变成helpful，在我的工具箱中有了合适的位置，随取随用，十分方便。我已经成了xpath的受益者。现在才觉得学习xpath真是很有趣、有用。
目前还有个小问题，无法使用纯粹的xpath语法解决。问题描述如下：
xpath只能解析实体内容，不能&#8221;囫囵吞枣&#8221;地解析。例如：

?View Code XML&#60;li&#62;
	&#60;a href='http://a.com'&#62;hello world&#60;/a&#62;
&#60;/li&#62;

在view xpath 下，使用/li/a，得到的是

?View Code XML&#60;a href='http://a.com'&#62;hello world&#60;/a&#62;

全部内容；
但是在python下，使用

?View Code PYTHONmethod=doc.xpath&#40;u'''string(/li/a)'''&#41;

虽然，也能通过/li/a/@href得到&#8217;http://a.com&#8217;的内容。
却只能得到hello world。xpath把所有的&#60;&#62;之内的东西给消灭掉了。很诡异。
遇到这种情况，如果我想得到整条的信息，就使用list.childNodes[index-1].firstChild.toxml()[22:-7]这种变通方式。不过，之前的doc = Parse(str(list.toxml()))我觉得用得挺好，是自己的一个&#8221;创举&#8221;，在程序中再度使用一下传统的xml解析方式，也无可厚非。当然，如果能够在xpath下把上述所有的事情都处理掉，是最好的。
经过了一点点的修补、改进，最终的饭否消息程序如下（核心代码部分）：

?View Code PYTHON    def __getMsgByPage__&#40;self,page&#41;:    [...]]]></description>
			<content:encoded><![CDATA[<h2>抛板砖，引白玉：为何不用xpath，什么是xpath？</h2>
<p>最近拾起了以前的小项目，在完善<a href="http://iregex.org/blog/fanfou-message-extractor-regex-vs-xml.html">上篇文章</a>发布后，“那个谁”的回复让我很感兴趣。他问，“为什么不用xpath？”</p>
<p>xpath是什么东东？我反问。反问之前，当然少不了先google一番，以免……那个啥。</p>
<p>首先映入眼帘的是<a href="http://www.w3c.org/TR/xpath">w3c</a> ，对xpath的介绍如下：  </p>
<blockquote><p>XPath is a language for addressing parts of an XML document, designed to be used by both XSLT and XPointer. </p></blockquote>
<p>直译为中文就是，</p>
<blockquote><p>XPath 是一种语言，用于在XML文档中定位各部分内容，可由XSLT或XPointer调用。</p></blockquote>
<p>还搜索到<a href="http://www.zvon.org/xxl/XPathTutorial/General/examples.html">xpath</a>的教程，在这里。草草看过，当时并未着意。</p>
<p>虽如此，但是python里的minidom模块，也有此功效呀。为什么非要使用xpath呢？尤其是考虑到在python中还需要额外安装，不如minidom之放之四海而皆可运行。</p>
<p>跟那个谁再交流，意见仍是“力荐”。还推荐我细读<a href="http://www.zvon.org/xxl/XPathTutorial/General/examples.html">教程</a>，并在firefox里使用<a href="https://addons.mozilla.org/zh-CN/firefox/addon/1095">XPath Checker</a>插件。</p>
<p>于是就照办了。</p>
<h2>发硎新试,其快可知</h2>
<p>一试XPath Checker，果然石破天惊。选中部分网页文字后，在右键菜单中选&#8221;View Xpath&#8221;，立即显示出该节点的XPath路径。层次清晰，定位精准。只是我对其语法尚未了了。于是细读教程，边学边用；半小时后，已经能够运用到之前写的饭否信息抓取程序上。虽然写代码还有些吃力，但是思路很清晰，不会纠缠于细节中无法脱身。</p>
<p>那个谁还提议，一般的html文档不是标准的xml文档，因此用xpath解析时，最好格式化一下。</p>
<p>我也注意到这个问题了。从饭否html中取出的有用内容，只占全文的一小部分；额外的部分白白拖慢速度，增强析取难度。</p>
<p>经过实验，我将原代码改进如下：</p>
<p>1. 仍用原来的minidom模块下载、分析文档，只取&lt;ol&gt;与&lt;/ol&gt;之间的部分。这部分保存成字符串格式，备用。只取需要的那部分，使结构清晰，层次浅显。</p>
<p>2. 使用xpath来解析上一步取出的字串。</p>
<p>到现在，/，//，@，[]，=，等等，每个符号都从原来的meaningless变成helpful，在我的工具箱中有了合适的位置，随取随用，十分方便。我已经成了xpath的受益者。现在才觉得学习xpath真是很有趣、有用。</p>
<p>目前还有个小问题，无法使用纯粹的xpath语法解决。问题描述如下：</p>
<p>xpath只能解析实体内容，不能&#8221;囫囵吞枣&#8221;地解析。例如：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p35code5'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p355"><td class="code" id="p35code5"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">'http://a.com'</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>hello world<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>在view xpath 下，使用/li/a，得到的是</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p35code6'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p356"><td class="code" id="p35code6"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">'http://a.com'</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>hello world<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>全部内容；</p>
<p>但是在python下，使用</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p35code7'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p357"><td class="code" id="p35code7"><pre class="python" style="font-family:monospace;">method=doc.<span style="color: black;">xpath</span><span style="color: black;">&#40;</span>u<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'string(/li/a)'</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>虽然，也能通过/li/a/@href得到&#8217;http://a.com&#8217;的内容。</p>
<p>却只能得到hello world。xpath把所有的&lt;&gt;之内的东西给消灭掉了。很诡异。</p>
<p>遇到这种情况，如果我想得到整条的信息，就使用list.childNodes[index-1].firstChild.toxml()[22:-7]这种变通方式。不过，之前的doc = Parse(str(list.toxml()))我觉得用得挺好，是自己的一个&#8221;创举&#8221;，在程序中再度使用一下传统的xml解析方式，也无可厚非。当然，如果能够在xpath下把上述所有的事情都处理掉，是最好的。</p>
<p>经过了一点点的修补、改进，最终的饭否消息程序如下（核心代码部分）：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p35code8'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p358"><td class="code" id="p35code8"><pre class="python" style="font-family:monospace;">    <span style="color: #ff7700;font-weight:bold;">def</span> __getMsgByPage__<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,page<span style="color: black;">&#41;</span>:        
&nbsp;
        url=<span style="color: #483d8b;">&quot;http://fanfou.com/&quot;</span>+<span style="color: #008000;">self</span>.<span style="color: #dc143c;">user</span>+<span style="color: #483d8b;">&quot;/p.&quot;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>page<span style="color: black;">&#41;</span>
        node = minidom.<span style="color: black;">parse</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">list</span> = node.<span style="color: black;">getElementsByTagName</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ol&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
        doc = Parse<span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">list</span>.<span style="color: black;">toxml</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        cu=<span style="color: #008000;">self</span>.<span style="color: black;">sql</span>.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">max</span>=doc.<span style="color: black;">xpath</span><span style="color: black;">&#40;</span>u<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'count(/ol/li)'</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">max</span>=<span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">max</span><span style="color: black;">&#41;</span>+<span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">max</span>==<span style="color: #ff4500;">1</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
        <span style="color: #008000;">max</span>=<span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">max</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> index <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #008000;">max</span><span style="color: black;">&#41;</span>:
            method=doc.<span style="color: black;">xpath</span><span style="color: black;">&#40;</span>u<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'string(/ol/li[%d]//span[@class='</span>method<span style="color: #483d8b;">'])'</span><span style="color: #483d8b;">''</span> <span style="color: #66cc66;">%</span> index<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span>	
            method=method.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' '</span>,<span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> method==<span style="color: #483d8b;">&quot;彩信&quot;</span>:
                <span style="color: #dc143c;">time</span>=doc.<span style="color: black;">xpath</span><span style="color: black;">&#40;</span>u<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'string(/ol/li[%d]//span[@class=&quot;time&quot;]/@title)'</span><span style="color: #483d8b;">''</span>\
                        <span style="color: #66cc66;">%</span> index<span style="color: black;">&#41;</span>
                uuid=doc.<span style="color: black;">xpath</span><span style="color: black;">&#40;</span>u<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'string(/ol/li[%d]//a[@class='</span>photo<span style="color: #483d8b;">']/@href)'</span><span style="color: #483d8b;">''</span>\
                        <span style="color: #66cc66;">%</span> index<span style="color: black;">&#41;</span> <span style="color: black;">&#91;</span>-<span style="color: #ff4500;">11</span>:<span style="color: black;">&#93;</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                <span style="color: #dc143c;">time</span>=doc.<span style="color: black;">xpath</span><span style="color: black;">&#40;</span>u<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'string(/ol/li[%d]//a[@class='</span><span style="color: #dc143c;">time</span><span style="color: #483d8b;">']/@title)'</span><span style="color: #483d8b;">''</span>\
                        <span style="color: #66cc66;">%</span> index<span style="color: black;">&#41;</span> 
                uuid=doc.<span style="color: black;">xpath</span><span style="color: black;">&#40;</span>u<span style="color: #483d8b;">''</span><span style="color: #483d8b;">'string(/ol/li[%d]//a[@class='</span><span style="color: #dc143c;">time</span><span style="color: #483d8b;">']/@href)'</span><span style="color: #483d8b;">''</span> <span style="color: #66cc66;">%</span> index<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>-<span style="color: #ff4500;">11</span>:<span style="color: black;">&#93;</span>
&nbsp;
            content = <span style="color: #008000;">list</span>.<span style="color: black;">childNodes</span><span style="color: black;">&#91;</span>index-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>.<span style="color: black;">firstChild</span>.<span style="color: black;">toxml</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">22</span>:-<span style="color: #ff4500;">7</span><span style="color: black;">&#93;</span>
            <span style="color: #808080; font-style: italic;"># content, uuid, time, method are now available for further use.</span></pre></td></tr></table></div>

<p>最关键的代码，只有几行而已。省掉了原来长篇累牍的coding。效率也错，我将自己近3000条饭否消息批量下载，共150余页，历时86秒。饭否服务器也很给面子，中途没有封锁我。</p>
<p><strong>总结一下</strong>：Xpath很适合在xml中定位各部分内容，定位精准，描述性极佳，是xml中的搜索利器。经常做xml解析的，不妨尝试一把。</p>
<h2>个人感言</h2>
<p>从纯手工正则表达式解析，到使用minidom解析，再到使用xpath，看似弯路，其实蛮有收获。从自己事必躬亲精确控制每一个细节（用手工作），再到借助工具实现一部分功能（手脑并用），再到完全用合适的工具来处理全部事情（用脑工作），似乎正是良性的发展路径。自豪地说，由于我已经使用过纯手工正则表达式的解析，即使现有的工具不适合我，我进可攻，退可守；我知道解析的细节，现有的工具（好看的封装而已嘛）骗不了我，即使它包装得再好，还是正则表达式在作引擎（曾经读过python处理xml的相关库文件的python代码，感谢开源）；从追求实现(it works!)到追求卓越的实现(the excellent solution)，也是进步的必然。我不是说使用正则式就低级——我从来没有说过诸如此类的话，不论是对正则表达式，还是对正则表达式的使用者；事实上，正则表达式一直是我的箧中飞刃；我爱正则表达式！——只是说，不同的工具在合适的场合，有不同的效用。不单要知道某种工具的缺点以便能够避其短，更重要的是要知道它的优点以便扬其长。这样才能从容地调兵遣将，手下无不可用之工具。</p>
<p>相关链接：</p>
<ul>
<li><a href="http://www.w3.org/TR/xpath">W3C关于XPath的介绍</a></li>
<li><a href="http://www.zvon.org/xxl/XPathTutorial/General/examples.html">xpath教程</a>，有中文版，图文并茂，清晰易懂。</li>
<li><a href="http://4suite.org">4suite</a>，python的xpath套件</li>
<li><a href="http://search.cpan.org/~samtregar/Class-XPath-1.4/XPath.pm">perl其实也有xpath的</a>。未测试试。</li>
<li><a href="https://addons.mozilla.org/zh-CN/firefox/addon/1095">XPath Checker</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/fanfou-message-extractor-from-minidom-to-xpath.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>两本关于正则表达式的PDF电子书</title>
		<link>http://iregex.org/blog/2-pdf-ebooks-about-regex.html</link>
		<comments>http://iregex.org/blog/2-pdf-ebooks-about-regex.html#comments</comments>
		<pubDate>Sun, 12 Oct 2008 05:29:57 +0000</pubDate>
		<dc:creator>rex</dc:creator>
		
		<category><![CDATA[resources]]></category>

		<category><![CDATA[tutorial]]></category>

		<category><![CDATA[pdf]]></category>

		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=34</guid>
		<description><![CDATA[1. Regular Expression HOWTO
作者：
A.M. Kuchling(amk@amk.ca)。
简要介绍：
本书主要介绍如何在python下使用re模块的正则表达式来处理问题。
本电子书是英文的，没有中文版。不过，内容比较简单，正则式和python的初学者可以看看。
不足：
（只针对书本身，不针对内容）虽然是pdf，虽然有目录，但是点击目录不能跳转。由于本书只有短短25页，一会就能看完。所以，还是将就看吧。
文件：
21页，pdf尺寸130kb。MD5：0a223f1ad3f932c8a0a7494120c10b05。
下载地址（如果您是通过FEED读本文，请移步到原始地址下载）：
另：关于python下使用正则式，还有两个链接可供参考，在这里：
python正则表达式链接
2. 正则表达式指南
作者：
张子阳，（博客http://jimmyzhang.cnblogs.com）。
简要介绍：
本电子书是中文版的，有概念有解释有例子，适合偏爱中文的初学者阅读。
本书提到了一款正则式测试工具，RegexTester，由于现在在ubuntu下，尚未安装wine，这款软件有时间再测试。先略过不谈。
不足之处：
对正则表达式的语言没有明确指出，或许作者的意图是写适合各种语言的正则表达式的教程吧。以笔者之见，初级程度的正则表达式区别不大，而深究起来，各种正则表达式的语言、工具各有其不同风格。本书忽略了这些特点，或许是为了不让初学者心存芥蒂，先了解正则表达式的概念。
文件：
45页，pdf，362kb。MD5：77e36c693bc2d3248fb0dcfd70250037。
下载地址（如果您是通过FEED读本文，请移步到原始地址下载）：
]]></description>
			<content:encoded><![CDATA[<h2><strong><span style="color: #ff008c;">1. Regular Expression HOWTO</span></strong></h2>
<p><strong>作者</strong>：</p>
<p>A.M. Kuchling(<a href="mailto:amk@amk.ca">amk@amk.ca</a>)。</p>
<p><strong>简要介绍</strong>：<br />
本书主要介绍如何在python下使用re模块的正则表达式来处理问题。<br />
本电子书是英文的，没有中文版。不过，内容比较简单，正则式和python的初学者可以看看。</p>
<p><strong>不足</strong>：<br />
（只针对书本身，不针对内容）虽然是pdf，虽然有目录，但是点击目录不能跳转。由于本书只有短短25页，一会就能看完。所以，还是将就看吧。</p>
<p><strong>文件</strong>：<br />
21页，pdf尺寸130kb。MD5：0a223f1ad3f932c8a0a7494120c10b05。</p>
<p><strong>下载地址</strong>（如果您是通过FEED读本文，请移步到<a href="http://iregex.org/blog/2-pdf-ebooks-about-regex.html">原始地址</a>下载）：<br />
Note: There is a file embedded within this post, please visit this post to download the file.</p>
<p>另：关于python下使用正则式，还有两个链接可供参考，在这里：<br />
<a href="http://iregex.org/blog/python-regular-expression.html">python正则表达式链接</a></p>
<h2><strong><span style="color: #ff008c;">2. 正则表达式指南</span></strong></h2>
<p><strong>作者</strong>：<br />
张子阳，（博客<a href="http://jimmyzhang.cnblogs.com">http://jimmyzhang.cnblogs.com</a>）。</p>
<p><strong>简要介绍</strong>：<br />
本电子书是中文版的，有概念有解释有例子，适合偏爱中文的初学者阅读。<br />
本书提到了一款正则式测试工具，RegexTester，由于现在在ubuntu下，尚未安装wine，这款软件有时间再测试。先略过不谈。</p>
<p><strong>不足之处</strong>：<br />
对正则表达式的语言没有明确指出，或许作者的意图是写适合各种语言的正则表达式的教程吧。以笔者之见，初级程度的正则表达式区别不大，而深究起来，各种正则表达式的语言、工具各有其不同风格。本书忽略了这些特点，或许是为了不让初学者心存芥蒂，先了解正则表达式的概念。</p>
<p><strong>文件</strong>：<br />
45页，pdf，362kb。MD5：77e36c693bc2d3248fb0dcfd70250037。</p>
<p><strong>下载地址</strong>（如果您是通过FEED读本文，请移步到<a href="http://iregex.org/blog/2-pdf-ebooks-about-regex.html">原始地址</a>下载）：<br />
Note: There is a file embedded within this post, please visit this post to download the file.</p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/2-pdf-ebooks-about-regex.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>饭否消息析取之regex vs xml</title>
		<link>http://iregex.org/blog/fanfou-message-extractor-regex-vs-xml.html</link>
		<comments>http://iregex.org/blog/fanfou-message-extractor-regex-vs-xml.html#comments</comments>
		<pubDate>Wed, 08 Oct 2008 10:53:59 +0000</pubDate>
		<dc:creator>rex</dc:creator>
		
		<category><![CDATA[tutorial]]></category>

		<category><![CDATA[fanfou]]></category>

		<category><![CDATA[perl]]></category>

		<category><![CDATA[python]]></category>

		<category><![CDATA[regex]]></category>

		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=33</guid>
		<description><![CDATA[页内导航：

能否只用官方的API来获取全部饭否消息？
饭否消息结构
使用regex解析饭否消息
使用xml解析饭否消息
两相比较
相关阅读


批量导出饭否程序的方法很多，但是基本思路都是先将该网页保存到本地，然后将有用的饭否消息析取出来。本文不讨论如何下载饭否网页了（使用迅雷、wget、curl等），重点讨论对于下载到本地的网页，如何将有用的饭否消息析取出来。

小插曲：能否只用官方的API来获取全部饭否消息？
您或许会提议为什么不使用饭否自身的API。是的，饭否的API更快捷方便，兼容性很强。只是，饭否官方只提供下载前20条饭否消息的API。如果纯粹使用饭否官方API来下载全部饭否消息的方法也不是没有，只是很邪恶：

?View Code PERLwhile &#40;true&#41; 
&#123; 
    download 20 messages via API; 
    store them; 
    delete this 20 messages via API; 
&#125;


一边下载一边删除，确实总能得到全部消息。删除了前面的20条，能保证后面的20条以新消息的面目出现。这在理论上是行得通的。但是我们需要的是英雄Heroes里Peter那样无损的复制方式，而不是Sylar那样的残忍的剪切方式，呵呵。既然官方的API有限制，我们就自己动手了。请继续阅读本文。

饭否消息结构
打开一个饭否消息网页的源代码，例如本人的
http://fanfou.com/regex/p.1（其实http://fanfou.com/regex是http://fanfou.com/regex/p.1的快捷方式。这里使用完整的路径，以便体现其一般性。），观察可见，有用的饭否消息在这个框架里面：（代码较长，阅读请点击展开）

?View Code XML 
&#60;ol&#62;
    &#60;li&#62;
        &#60;span class=&#34;content&#34;&#62;
          [...]]]></description>
			<content:encoded><![CDATA[<p>页内导航：</p>
<ul>
<li><a href="#xiaochaqu"><strong>能否只用官方的API来获取全部饭否消息？</strong></a></li>
<li><a href="#饭否消息结构"><strong>饭否消息结构</strong></a></li>
<li><a href="#regex"><strong>使用regex解析饭否消息</strong></a></li>
<li><a href="#python"><strong>使用xml解析饭否消息</strong></a></li>
<li><a href="#compare"><strong>两相比较</strong></a></li>
<li><a href="#xiangguan"><strong>相关阅读</strong></a></li>
</ul>
<p>
批量导出饭否程序的方法很多，但是基本思路都是先将该网页保存到本地，然后将有用的饭否消息析取出来。本文不讨论如何下载饭否网页了（使用迅雷、wget、curl等），重点讨论对于下载到本地的网页，如何将有用的饭否消息析取出来。<br />
<blockquote>
<h2><a href="#xiaochaqu"><strong><span style="color: #ff008c;">小插曲：能否只用官方的API来获取全部饭否消息？</span></strong></a></h2>
<p>您或许会提议为什么不使用饭否自身的API。是的，饭否的API更快捷方便，兼容性很强。只是，饭否官方只提供下载前20条饭否消息的API。如果纯粹使用饭否官方API来下载全部饭否消息的方法也不是没有，只是很邪恶：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p33code13'); return false;">View Code</a> PERL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p3313"><td class="code" id="p33code13"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>true<span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span> 
    download <span style="color: #cc66cc;">20</span> messages via API<span style="color: #339933;">;</span> 
    store them<span style="color: #339933;">;</span> 
    <span style="color: #000066;">delete</span> this <span style="color: #cc66cc;">20</span> messages via API<span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p>一边下载一边删除，确实总能得到全部消息。删除了前面的20条，能保证后面的20条以新消息的面目出现。这在理论上是行得通的。但是我们需要的是英雄Heroes里Peter那样无损的复制方式，而不是Sylar那样的残忍的剪切方式，呵呵。既然官方的API有限制，我们就自己动手了。请继续阅读本文。</p>
</blockquote>
<h2>饭否消息结构</h2>
<p>打开一个饭否消息网页的源代码，例如本人的<br />
<a name="饭否消息结构"></a><a title=" 我爱正则表达式" href="http://fanfou.com/regex" target="_blank">http://fanfou.com/regex/p.1</a>（其实http://fanfou.com/regex是http://fanfou.com/regex/p.1的快捷方式。这里使用完整的路径，以便体现其一般性。），观察可见，有用的饭否消息在这个框架里面：（代码较长，阅读请点击展开）</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p33code14'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p3314"><td class="code" id="p33code14"><pre class="xml" style="font-family:monospace;"> 
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ol<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;content&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            代码非抄不能懂也。<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;stamp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;time&quot;</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;2008-10-03 12:07&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;/statuses/QD6qHiqUbeE&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                2008-10-03 12:07<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;method&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                通过 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://del.icio.us/fanfou/API%E5%BA%94%E7%94%A8&quot;</span> <span style="color: #000066;">target</span>=<span style="color: #ff0000;">&quot;_blank&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    API<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;content&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            向自由的身心致敬！ - 早嗷嗷也盼~晚安安也盼~望穿安安双眼~~怎知道今日里打土匪进深山自己的队伍来哎到嗷~面安前安呐啊啊啊啊啊~~~ <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://fanfou.com/linkto/aHR0cDovL3d3dy5kb3ViYW4uY29tL2V2ZW50LzEwMjczNDg3Lw&quot;</span> <span style="color: #000066;">target</span>=<span style="color: #ff0000;">&quot;_blank&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                http://www.douban.com/event/10273487/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;stamp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;time&quot;</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;2008-10-06 14:07&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;/share/bd96z1U-gHw&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                2008-10-06 14:07<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;method&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                通过<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://help.fanfou.com/share_button.html&quot;</span> <span style="color: #000066;">target</span>=<span style="color: #ff0000;">&quot;_blank&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    饭否分享<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;content&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;photo&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://fanfou.com/photo/8JsezhHM_VU&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://photo.fanfou.com/m0/00/19/e2_36807.jpg&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;caixinceshi - no description&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            上传了新照片：caixinceshi - no description<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;stamp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;time&quot;</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;2008-10-03 11:33&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                2008-10-03 11:33<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;span</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;method&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                通过<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://help.fanfou.com/mobile_mms.html&quot;</span> <span style="color: #000066;">target</span>=<span style="color: #ff0000;">&quot;_blank&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    彩信<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    #更多的<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>...<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>条目，每页最多20条。
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ol<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Tips：在分析饭否源代码时，饭否消息全在一行，不便于阅读。您可以拷贝所需要的代码（注意前后结构的匹配呼应）到vim中，执行<tt class="string">:%s/&gt;/&gt;\r/g</tt>(将每个&gt;后面加上一个换行符)，再按<tt class="string">ggvG</tt>全选，按<tt class="string">=</tt>格式代码，所有的代码就成了漂亮的缩进格式，便于阅读了。</p>
</p>
<p><a name="regex"><br />
<h2>使用regex解析饭否消息</h2>
<p> </a></p>
<p>下面是使用regex来解析饭否消息的代码（直接拷贝自本人原来的perl抓饭程序。）</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p33code15'); return false;">View Code</a> PERL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p3315"><td class="code" id="p33code15"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ffmsg</span><span style="color: #339933;">=</span><span style="color: #000066;">qr</span><span style="color: #009900;">&#123;</span>
    <span style="color: #009999;">&lt;li&gt;</span>
        <span style="color: #339933;">&lt;</span>span class<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;content&quot;</span><span style="color: #339933;">&gt;</span>
            <span style="color: #009900;">&#40;</span>.<span style="color: #339933;">*?</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>span class<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;stamp&quot;</span><span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;/(?:statuses|share)/([-_a-zA-Z0-9]{11})&quot;</span> class<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;time&quot;</span> title<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;([-: 0-9]{16})&quot;</span><span style="color: #339933;">&gt;</span>
                <span style="color: #009900;">&#91;</span>^<span style="color: #339933;">&lt;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>
            <span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> 
            <span style="color: #339933;">&lt;</span>span class<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;method&quot;</span><span style="color: #339933;">&gt;</span>
                通过<span style="color: #009900;">&#40;</span>网页<span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?:</span>\<span style="color: #000066;">s</span><span style="color: #339933;">*&lt;</span><span style="color: #009900;">&#91;</span>^<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>^<span style="color: #339933;">&lt;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?:&lt;/</span>a<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;/</span>span<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #ff0000;">' }xi;</span></pre></td></tr></table></div>

</p>
<p>可以看出，使用正则表达式，能够比较真实地再现原网页代码的风貌。有几处小地方需要说明一下：</p>
<ul>
<li>在第一组小括号里，我使用了<tt class="regex">([^<]+?)</tt>来捕获消息正文（一条完整的消息可以分为：消息正文；发送时间；消息uuid例如QD6qHiqUbeE，发送方法，类型（彩信还是文本））。最初是使用<tt class="regex">.*?</tt>的。但是这样不精确，有时候两条消息竟然混合在一起。而<tt class="regex">([^<]+?)</tt>捕获的是从当前位置开始至下一个&lt;之前的所有内容。或许您会问，这不怕受到消息正文中可能出现的&lt;的影响吗？答案是：不会受到影响。因为饭否会把所有的&lt;以及其实有可能影响解析的字符，都转换成&lt;的形式了，因此它不影响解析。同时，<strong>使用精确的正则表达式有助于提高效率，让不匹配的正则式尽早失败。</strong></li>
<li><tt class="regex">(?:statuses|share)</tt>。这条正则表达式是用来捕获饭否的uuid。它不但能捕获以普通方法发布的消息（网页、短信、手机、API、IM工具等），还能捕获由“饭否分享”工具发布的消息。我不是很喜欢饭否分享这个工具。（或许改天有时间写篇文章，揭露它的缺点？）之所以把“饭否分享”消息和普通消息分开来说，是因为两者的结构是不一样的。</li>
<li>通过<tt class="regex">(网页|(?:\s*<[^>]+>)[^<]+(?:))</tt>这条正则式，既用了捕获型括号，又用了非捕获型括号。使用后者，能有效地避免程序太复杂，便于按序号引用（$1,$2等，如果越多则越混乱，修改正则式后，更是乱成一团遭），还能节省内存（如果程序中捕获了太多的内容，而不及时释放，或许会占尽资源。毕竟不是只捕获几十字节。要考虑到饭否用户或许有近十万条的饭否消息。指的是<a href="http://fanfou.com/appleice">苹果流冰</a>这样的“万玻南痨话”）</li>
<li><tt class="regex">xi</tt>选项：<tt class="regex">x</tt>是为了使用忽略空白字符和允许注释；<tt class="regex">i</tt>选项是忽略大小写。</li>
</ul>
<p>使用正则表达式来析取饭否消息文本，需要考虑的细节很多。一处不细致，程序运行起来就会给你难看。饭否彩信的格式就略过不分析了。道理相同，点到为止。</p>
</p>
<p><a name="python"><br />
<h2>使用xml解析饭否消息</h2>
<p></a></p>
<p>再来看一下在python下，使用xml来解析饭否消息。注：该程序参考了<a href="http://www.happysky.org/" target="_blank"><strong><span style="color: #ff008c;">ppip</span></strong</a>的<a href="http://code.google.com/p/pyfan/" target="_blank">pyfan</a>程序。<br />
 </p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p33code16'); return false;">View Code</a> PYTHON</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p3316"><td class="code" id="p33code16"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">dom</span> <span style="color: #ff7700;font-weight:bold;">import</span> minidom, Node <span style="color: #808080; font-style: italic;">#引人解析工具：xml小马驹！ </span>
node = minidom.<span style="color: black;">parse</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;http://fanfou.com/zhasm/p.1&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> 
<span style="color: #808080; font-style: italic;">#抓取页面http://fanfou.com/zhasm/p.1 的全部内容到变量node中 </span>
l = node.<span style="color: black;">getElementsByTagName</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ol&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> 
<span style="color: #808080; font-style: italic;">#将饭否消息部分内容保存到变量l中 </span>
<span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, number<span style="color: black;">&#41;</span>: 
    <span style="color: #808080; font-style: italic;"># 时间 </span>
    <span style="color: #ff7700;font-weight:bold;">if</span> l.<span style="color: black;">childNodes</span><span style="color: black;">&#91;</span>c<span style="color: black;">&#93;</span>.<span style="color: black;">hasAttribute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;class&quot;</span><span style="color: black;">&#41;</span>: 
        <span style="color: #ff7700;font-weight:bold;">continue</span> 
    content = <span style="color: black;">&#40;</span> 
            <span style="color: #808080; font-style: italic;">#时间: </span>
            l.<span style="color: black;">childNodes</span><span style="color: black;">&#91;</span>c<span style="color: black;">&#93;</span>.<span style="color: black;">childNodes</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span>.\ 
            firstChild.<span style="color: black;">getAttribute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;title&quot;</span><span style="color: black;">&#41;</span>, 
            <span style="color: #808080; font-style: italic;">#消息正文 :</span>
            childNodes<span style="color: black;">&#91;</span>c<span style="color: black;">&#93;</span>.<span style="color: black;">childNodes</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">toxml</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">22</span>:-<span style="color: #ff4500;">7</span><span style="color: black;">&#93;</span>, 
            <span style="color: #808080; font-style: italic;">#uuid </span>
            l.<span style="color: black;">childNodes</span><span style="color: black;">&#91;</span>c<span style="color: black;">&#93;</span>.<span style="color: black;">childNodes</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span>.<span style="color: black;">firstChild</span>.\ 
            getAttribute<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;href&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">10</span>:<span style="color: black;">&#93;</span>
            <span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>在xml文件中，前后呼应的标签，成了鲜活的特征，这些特征可以被xml解析函数很容易地辨识出来，并提取出所需内容。</p>
<ul>
<li><strong>childNodes[c].childNodes[0].toxml()[22:-7]</strong>：这条语句的意思是，对于每一条饭否消息（childNodes[c]），其消息内容的第一个节点（childNodes[0]），截取其第23字节到倒数第7字节的内容。它是指哪一段呢？其实就是每一对&lt;span class=&#8221;content&#8221;&gt;&#8230;&lt;/span&gt;之间点号所示的内容。</li>
<li>每条消息的发送时间、正文、uuid，保存在tuple中。</li>
</ul>
<p>取得了内容之后，至于之后的煎炒烹炸，就悉听尊便了。</p>
<p>值得一提的是，本人在大量下载饭否消息时，不止一次遇到过饭否页面无法访问的情况。问了饭否郭万怀，答曰为了减轻服务器负载，每个IP地址下每分钟允许访问100个页面。超过此数就会自动屏蔽。我测试的结果是少于100页。比较靠谱的间隔是，每析取一页，sleep(15)。是有些慢了。没办法。当然，也有人说，执行本人以前写的抓饭程序，一次下载几百页，并没有遇到当机情况。那我只能说是您的RP高、运气好了。</p>
</p>
<p><a name="compare"><br />
<h2>两者比较</h2>
<p></a> </p>
<p>个人认为，xml与regex相比，有如下特点：</p>
<ul>
<li><strong>通用性：</strong>xml具有通用性，不单单能解析饭否消息，其它符合规则的html文本，同样能够较少地改动代码，即可解析；而正则表达式则具有专用性，不能放之四海而皆准。当饭否的界面、框架有微调时，估计使用正则表达式解析的工具首先倒下。</li>
<li><strong>可读性：</strong>有人说perl是只写语言，regex尤甚。这是在说perl或regex代码在编写时性之所至，酣畅淋漓，执行也很高效。只是，如果代码格式混乱且无注释文档的话，隔数日、数月再读，仿佛读天书一般。而使用xml库来解析的python语言，则由于代码格式整齐，库函数见名知意，因而具有较强的可读性。这样说，总体是这样。不过我们可以尽可能把代码（即使是perl或regex的）写的整齐已读，尤其是考虑到perl支持<tt class="regex">/x</tt>选项。</li>
<li><strong>效率：</strong>良好编译的正则式，其执行效率应该优于xml解析。但是，使用xml能够节省编程时间；使用正则式牺牲一部分的编程时间，理论上能提高一点点效率。有兴趣的读者可以编写一段程序，循环个成千上万次，比较一下平均时间。</li>
</ul>
<p>
写到这里，对照金庸先生在《鹿鼎记》第五章：“金戈运启驱除会，玉匣书留想象间”两种武功的比较，颇有意味：<br />
“大慈大悲千叶手”招式太多，记起来麻烦。而“八卦游龙掌”只有八八六十四式，但反复变化，尽可敌得住千叶手。那么哪一门功夫厉害些？这两门都是上乘掌法，说不上哪一门功夫厉害。谁的功夫深，用得巧妙，谁就胜了。
<p>以本文来看，regex就相当于是大慈大悲千叶手了，需要留意的细节太多；xml方式呢，就相当于只有八八六十四式的“八卦游龙掌”。两种工具都很有用。</P></p>
<h2>呼吁官方提供更多功能</h2>
<p>离题了。这里顺便发发牢骚而已，与xml、regex无关。我不止一次地在饭否和本人blog中抱怨，使用上面这足粗笨的方法下载、解析，是最无奈的应用。最便捷的方式，应该是官方提供批量导出程序，只要执行一条数据库查询导出即可实现我们辛辛苦苦半天才能以变通的方式实现的功能。或许是饭否官方的人员都在忙着增强和美化海内吧，饭否自生自长，长时间没有更新，任凭jiwai.de、zuosa等推出一项又一项的新功能。 </p>
</p>
<h2>扩展</h2>
<p>本文的思路，对twitter同样适用。但是twitter越来越慢了。有一段时间好像还不支持查看历史页面。</p>
</p>
<p><a name="xiangguan"><br />
<h2>相关阅读</h2>
<p></a></p>
<ul>
<li><a href="http://iregex.org/blog/fanfou-private-message-format-analysis.html" target="_blank">饭否私信格式分析</a></li>
<li><a href="http://zhasm.com/blog/fanfou-msg-grabber-limitation-and-suggestion-on-sharing-msg.html">关于饭否消息打包下载的限制以及对于饭否分享功能的建议</a></li>
<li><a href="http://zhasm.com/blog/about-my-fanfou-applications.html">关于本人编写的饭否应用的三言两语</a></li>
<li><a href="http://zhasm.com/blog/comments-on-fanfou.html">饭否，尚能饭否？</a></li>
<li><a href="http://zhasm.com/blog/uuid-in-twitter-and-fanfou.html">uuid in twitter and fanfou</a></li>
<li><a href="http://zhasm.com/blog/fanfou-message-grabber.html">批量抓饭脚本：一次性打包输出自己全部的饭否消息！</a></li>
<li><a href="http://zhasm.com/blog/fanfou-vs-twitter-base64-vs-tinyurl.html">fanfou vs twitter, base64 vs tinyurl?</a></li>
</ul>
<p><span style="color: #ffffff;">验证码：BANG1F79A9FAD20225BEA7FE397AXIANGUO e8da37692b5b030cbefb9956e3bdb9cc</span></p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/fanfou-message-extractor-regex-vs-xml.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>windows下的正则式工具介绍之三：MTracer2.0介绍以及与RegexBuddy比较</title>
		<link>http://iregex.org/blog/mtracer-vs-regexbuddy.html</link>
		<comments>http://iregex.org/blog/mtracer-vs-regexbuddy.html#comments</comments>
		<pubDate>Tue, 16 Sep 2008 03:29:13 +0000</pubDate>
		<dc:creator>rex</dc:creator>
		
		<category><![CDATA[工具软件]]></category>

		<category><![CDATA[chinese]]></category>

		<category><![CDATA[mtracer]]></category>

		<category><![CDATA[powergrep]]></category>

		<category><![CDATA[regexbuddy]]></category>

		<category><![CDATA[tool]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=32</guid>
		<description><![CDATA[RegexBuddy和PowerGrep是我在windows下常用的两款正则式工具。前者是帮助编写正则式的辅助工具，后者是进行批量搜索替换的实用工具。这两款软件都是外国人写的。今天介绍国人史寿伟先生写的一款正则式工具，MTracer 2.0。 
&#160;
MTracer 2.0软件版本
MTracer2.0全名是RegexMatchTracer，官方主页在regexlab.com。上面说最近更新日期是2007.10.07，不过，从我今天下载的程序的修改日期来看，是2008.09.13。变化是，之前作者提供的是绿色的程序，现在提供的是msi的安装包。 本文以下提到MTracer时，若无特殊说明，均指MTracer2.0。
&#160;
MTracer软件特性
作为一款正则表达式撰写辅助程序，它拥有查找匹配、替换模式、分割模式，可以分别进行相应操作。前两种用的比较多，各种语言或正则式工具都有相应语句或函数来实现；第三种分割模式是指使用正则表达式来描述字符串的分割符，以便将字串分割成子串数组。举个简单的例子来说，可以使用正则式\d+;?将abcd12;sdf55656asdfasd82asd33x字串分割成子串数组：

abcd 
sdf 
asdfasd 
asd 
x 

在实际操作时，你总会遇到使用分割模式才能最有效率地解决的问题。
 除了一般的选项（忽略大小写、单行\多行、全局），它提供了从右向左、扩展模式两种匹配选项。
从右向左：平时查找字串最右边的正则式匹配，可以借助于?和$来定位。而在MTracer下，这款比较令人耳目一新的选项十分好玩，虽然我还没有在实际操作时遇到过确需此选项的例子。
扩展模式：包括如下选项：

注释 (?#xxx)：在正则式中加入注释以便提高可读性； 
模式修改符 (?ismg-ismg)：小范围内修改匹配模式； 
非捕获组 (?:xxx)：匹配而不捕获，便于计数，同时还可以节省内存，提高效率（根据《精通正则表达式》的说法）； 
预搜索（零宽度断言）：十分有用的匹配选项，只匹配位置，而不消耗字符；有四种模式，详见手册； 
独立表达式 (?&#62;pattern)：此选项在《精通正则表达式》中，被余晟先生翻译为&#8220;固化分组&#8221;，即无论匹配成功与否，内部都不进行回退，都不会再次尝试匹配； 
条件表达式(?(x)y&#124;z)：与C语言的三目操作符类似，x条件为真则进行y匹配，否则进行z匹配。其中x部分有四种模式，详见手册； 
递归表达式 (?R)：对另一部分子表达式的引用，而不是对其匹配结果的引用。当被引用的表达式包含自身，则形成递归引用。 

个人比较常用的选项是非捕获组和预搜索。
&#160;
MTracer最有用的特性
 相对于另一款正则式撰写辅助程序RegexBuddy而言，MTracer最有用的特性是中文的正则式分析树。虽然RegexBuddy也有此功能，但是MTracer的正则式分析树是中文的。这对于需要此功能但是不愿意使用英文软件的用户来说非常方便。对于初学者也很有帮助。
值得说明的是，正则式的中文术语在中文里还没有形成统一的规范。以我自身的阅读经历而言，似可分为两类：何伟平译的《Perl Programming》第三版中大量使用的正则式术语译法为第一套方案，余晟先生译的《精通正则表达式》第三版中使用的正则表达式为第二套。个人倾向于第二套。
MTracer使用的正则表达式术语，与这两套术语均有相异之处。
如果有人使用上述第二套术语将RegexBuddy汉化的话，估计在中国的普及率会大幅提高。题外话。
&#160;
&#160;
与RegexBuddy的简单比较
还是列表说明吧。



属性
MTracer
RegexBuddy


界面语言
中文
英文


大小
471kb
9.1 MB


价格(单用户)
个人RMB49.00
        公司RMB298.00
US$ 39.95


免费版的限制
正则式长度限制
7天免费使用时间


帮助文件
原来版本有，最新的msi版已经不带帮助文件；需要的话可以下载本站上传的手册。
齐全。包括4本电子书，其中3本是关于正则表达式的教程、参考手册，1本是RegexBuddy的操作手册。十分翔实。语言是英文。


匹配模式


匹配 
替换 
分割 




匹配 
替换 
分割 




匹配选项


忽略大小写（开关） 
单行、多行 
全局局部可选 
从右至左 
扩展模式（详见上文） 




忽略大小写 （开关） 
单行、多行 
默认全局 
点号匹配新行 
^$匹配换行符 
空白字符宽松模式 
扩展模式（依语言而定） 




历史功能
&#8220;文本片断&#8221;即是
History


导出字串为指定语言


原状导出 
Visual [...]]]></description>
			<content:encoded><![CDATA[<p><img style="border-bottom: rgb(255,255,255) 1px solid; border-left: rgb(255,255,255) 1px solid; margin: 0px 10px 10px; padding-left: 0px; float: right; clear: both; border-top: rgb(255,255,255) 1px solid; border-right: rgb(255,255,255) 1px solid" src="http://www.regexlab.com/images/logo_s.gif" />RegexBuddy和PowerGrep是我在windows下常用的两款正则式工具。前者是帮助编写正则式的辅助工具，后者是进行批量搜索替换的实用工具。这两款软件都是外国人写的。今天介绍国人<a target="_blank" href="http://www.softreg.com.cn/Search.aspx?t=authorid&amp;v=/efff07ff-b552-4034-8860-1964f285c4fc/">史寿伟先生</a>写的一款正则式工具，MTracer 2.0。 </p>
<p>&#160;</p>
<h2>MTracer 2.0软件版本</h2>
<p>MTracer2.0全名是RegexMatchTracer，官方主页在regexlab.com。上面说最近更新日期是2007.10.07，不过，从我今天下载的程序的修改日期来看，是2008.09.13。变化是，之前作者提供的是绿色的程序，现在提供的是msi的安装包。 本文以下提到MTracer时，若无特殊说明，均指MTracer2.0。</p>
<p>&#160;</p>
<h2>MTracer软件特性</h2>
<p><img style="border-bottom: rgb(255,255,255) 1px solid; border-left: rgb(255,255,255) 1px solid; margin: 0px 10px 10px; padding-left: 0px; float: right; clear: both; border-top: rgb(255,255,255) 1px solid; border-right: rgb(255,255,255) 1px solid" src="http://i3.6.cn/cvbnm/61/db/85/73daa684cce5f9fe1d6f327aa5d5502c.jpg" />作为一款正则表达式撰写辅助程序，它拥有查找匹配、替换模式、分割模式，可以分别进行相应操作。前两种用的比较多，各种语言或正则式工具都有相应语句或函数来实现；第三种<strong>分割模式</strong>是指<strong>使用正则表达式来描述字符串的分割符，以便将字串分割成子串数组。</strong>举个简单的例子来说，可以使用正则式<tt class="regex">\d+;?</tt>将<tt class="string">abcd12;sdf55656asdfasd82asd33x</tt>字串分割成子串数组：</p>
<ul>
<li>abcd </li>
<li>sdf </li>
<li>asdfasd </li>
<li>asd </li>
<li>x </li>
</ul>
<p>在实际操作时，你总会遇到使用分割模式才能最有效率地解决的问题。</p>
<p><img style="border-bottom: rgb(255,255,255) 1px solid; border-left: rgb(255,255,255) 1px solid; margin: 0px 10px 10px; padding-left: 0px; float: right; clear: both; border-top: rgb(255,255,255) 1px solid; border-right: rgb(255,255,255) 1px solid" src="http://i3.6.cn/cvbnm/df/9f/96/cfd4162eebd15ed35237a49bf94b82ae.jpg" /> 除了一般的选项（忽略大小写、单行\多行、全局），它提供了从右向左、扩展模式两种匹配选项。</p>
<p><strong>从右向左</strong>：平时查找字串最右边的正则式匹配，可以借助于<tt class="regex">?</tt>和<tt class="regex">$</tt>来定位。而在MTracer下，这款比较令人耳目一新的选项十分好玩，虽然我还没有在实际操作时遇到过确需此选项的例子。</p>
<p><strong>扩展模式</strong>：包括如下选项：</p>
<ul>
<li>注释 <tt class="regex">(?#xxx)</tt>：在正则式中加入注释以便提高可读性； </li>
<li>模式修改符 <tt class="regex">(?ismg-ismg)</tt>：小范围内修改匹配模式； </li>
<li>非捕获组 <tt class="regex">(?:xxx)</tt>：匹配而不捕获，便于计数，同时还可以节省内存，提高效率（根据《精通正则表达式》的说法）； </li>
<li>预搜索（零宽度断言）：十分有用的匹配选项，<strong>只匹配位置，而不消耗字符</strong>；有四种模式，详见<a target="_blank" href="http://www.mediafire.com/?ugf3z8tklbl">手册</a>； </li>
<li>独立表达式 <tt class="regex">(?&gt;pattern)</tt>：此选项在《精通正则表达式》中，被余晟先生翻译为&#8220;固化分组&#8221;，即无论匹配成功与否，内部都不进行回退，都不会再次尝试匹配； </li>
<li>条件表达式<tt class="regex">(?(x)y|z)</tt>：与C语言的三目操作符类似，x条件为真则进行y匹配，否则进行z匹配。其中x部分有四种模式，详见<a target="_blank" href="http://www.mediafire.com/?ugf3z8tklbl">手册</a>； </li>
<li>递归表达式 <tt class="regex">(?R)</tt>：对另一部分子表达式的引用，而不是对其匹配结果的引用。当被引用的表达式包含自身，则形成递归引用。 </li>
</ul>
<p>个人比较常用的选项是非捕获组和预搜索。</p>
<p>&#160;</p>
<h2>MTracer最有用的特性</h2>
<p><img style="border-bottom: rgb(255,255,255) 1px solid; border-left: rgb(255,255,255) 1px solid; margin: 0px 10px 10px; padding-left: 0px; float: right; clear: both; border-top: rgb(255,255,255) 1px solid; border-right: rgb(255,255,255) 1px solid" src="http://i3.6.cn/cvbnm/1c/a8/f0/2fed4bd2ff41a771aecc1558210c5442.jpg" /> 相对于另一款正则式撰写辅助程序RegexBuddy而言，MTracer最有用的特性是中文的正则式分析树。虽然RegexBuddy也有此功能，但是MTracer的正则式分析树是中文的。这对于需要此功能但是不愿意使用英文软件的用户来说非常方便。对于初学者也很有帮助。</p>
<p>值得说明的是，正则式的中文术语在中文里还没有形成统一的规范。以我自身的阅读经历而言，似可分为两类：何伟平译的《Perl Programming》第三版中大量使用的正则式术语译法为第一套方案，余晟先生译的《精通正则表达式》第三版中使用的正则表达式为第二套。个人倾向于第二套。</p>
<p>MTracer使用的正则表达式术语，与这两套术语均有相异之处。</p>
<p>如果有人使用上述第二套术语将RegexBuddy汉化的话，估计在中国的普及率会大幅提高。题外话。</p>
<p>&#160;</p>
<h2>&#160;</h2>
<h2>与RegexBuddy的简单比较</h2>
<p>还是列表说明吧。</p>
<table border="1" cellspacing="1" cellpadding="2" width="401">
<tbody>
<tr>
<td width="105" align="center"><strong>属性</strong></td>
<td width="132" align="center"><strong>MTracer</strong></td>
<td width="158" align="center"><strong>RegexBuddy</strong></td>
</tr>
<tr>
<td width="103">界面语言</td>
<td width="132">中文</td>
<td width="159">英文</td>
</tr>
<tr>
<td width="102">大小</td>
<td width="132">471kb</td>
<td width="160">9.1 MB</td>
</tr>
<tr>
<td width="103">价格(单用户)</td>
<td width="131">个人RMB49.00<br />
        <br />公司RMB298.00</td>
<td width="160">US$ 39.95</td>
</tr>
<tr>
<td width="103">免费版的限制</td>
<td width="131">正则式长度限制</td>
<td width="160">7天免费使用时间</td>
</tr>
<tr>
<td width="103">帮助文件</td>
<td width="131">原来版本有，最新的msi版已经不带帮助文件；需要的话可以下载本站上传的<a target="_blank" href="http://www.mediafire.com/?ugf3z8tklbl">手册</a>。</td>
<td width="160">齐全。包括4本电子书，其中3本是关于正则表达式的教程、参考手册，1本是RegexBuddy的操作手册。十分翔实。语言是英文。</td>
</tr>
<tr>
<td width="103">匹配模式</td>
<td width="131">
<ol>
<li>匹配 </li>
<li>替换 </li>
<li>分割 </li>
</ol>
</td>
<td width="160">
<ol>
<li>匹配 </li>
<li>替换 </li>
<li>分割 </li>
</ol>
</td>
</tr>
<tr>
<td width="103">匹配选项</td>
<td width="131">
<ol>
<li>忽略大小写（开关） </li>
<li>单行、多行 </li>
<li>全局局部可选 </li>
<li>从右至左 </li>
<li>扩展模式（详见上文） </li>
</ol>
</td>
<td width="160">
<ol>
<li>忽略大小写 （开关） </li>
<li>单行、多行 </li>
<li>默认全局 </li>
<li>点号匹配新行 </li>
<li>^$匹配换行符 </li>
<li>空白字符宽松模式 </li>
<li>扩展模式（依语言而定） </li>
</ol>
</td>
</tr>
<tr>
<td width="103">历史功能</td>
<td width="131">&#8220;文本片断&#8221;即是</td>
<td width="160">History</td>
</tr>
<tr>
<td width="103">导出字串为指定语言</td>
<td width="131">
<ol>
<li>原状导出 </li>
<li>Visual Basic </li>
<li>C/C++ </li>
</ol>
</td>
<td width="160">
<ol>
<li>原状导出 </li>
<li>C/C# </li>
<li>Perl(m//或s///格式) </li>
<li>Basic </li>
<li>Java </li>
<li>JavaScript </li>
<li>Pascal </li>
<li>PHP (//) </li>
<li>PostgreSQL </li>
<li>Python </li>
<li>Ruby </li>
<li>SQL </li>
<li>Tcl </li>
<li>XML </li>
<li><strong>还包含在上述语言中如何调用该正则式的模块，功能强大、有用、好用。（Use）</strong> </li>
</ol>
</td>
</tr>
<tr>
<td width="103">常用正则库</td>
<td width="131">帮助文件中有提及</td>
<td width="160">程序中包含该功能，有定义、例代码、匹配实例。<br />
        <br />帮助文件中也有。</td>
</tr>
<tr>
<td width="103">扩展性</td>
<td width="131">支持插件（疑安装目录下的stdplgin.dll是其插件，功能不详。）</td>
<td width="160">与PowerGrep相关联。</td>
</tr>
<tr>
<td width="103">帮助论坛</td>
<td width="131">开放，地址<a target="_blank" href="http://www.regexlab.com/zh/discuss/forum.aspx?b=1">在此</a></td>
<td width="160">仅对付费注册软件的用户开放。</td>
</tr>
<tr>
<td width="103">界面高度可定制</td>
<td width="132">不支持</td>
<td width="161">支持</td>
</tr>
<tr>
<td width="103">是否支持批量替换外部文件</td>
<td width="132">不支持</td>
<td width="161">支持</td>
</tr>
</tbody>
</table>
<p>当然，没有提到的功能、细节还不少，这里仅仅是列出本人关注的属性。</p>
<p>&#160;</p>
<h2>MTracer 2.0注册</h2>
<p>付出劳动，换来报酬，是当今任何行业都认可的规则。MTracer需要注册费，这一点也十分正常。其价格是48元人民币。比起US$ 39.95的RegexBuddy来，可谓性价比极高。</p>
<p><img style="border-bottom: rgb(255,255,255) 1px solid; border-left: rgb(255,255,255) 1px solid; margin: 0px 10px 10px; padding-left: 0px; float: right; clear: both; border-top: rgb(255,255,255) 1px solid; border-right: rgb(255,255,255) 1px solid" src="http://i3.6.cn/cvbnm/a7/08/a1/f3e2daa8f96c1c47d4b08ef7a6c14224.jpg" /> </p>
<p>如果不注册，会有什么限制呢？请看图。哦，是有100个字符的限制。</p>
<p><img style="border-bottom: rgb(255,255,255) 1px solid; border-left: rgb(255,255,255) 1px solid; margin: 0px 10px 10px; padding-left: 0px; float: right; clear: both; border-top: rgb(255,255,255) 1px solid; border-right: rgb(255,255,255) 1px solid" src="http://i3.6.cn/cvbnm/4c/d6/e2/5dcb18e7a4610987954ee363624a3584.jpg" /> 这是注册后的图。</p>
<p>对于未注册版本，平时写一些短小的正则式，是没有问题的。如果想无限制地使用该程序，花48元支持一下国产程序，何乐而不为？</p>
<p>现在无论主动还是被动，版权意识都在深入人心。这也是本人一直没有把完全版的RegexBuddy和PowerGrep的下载链接贴到blog上、而是采取来Email索取的原因之一。</p>
<p><img style="border-bottom: rgb(255,255,255) 1px solid; border-left: rgb(255,255,255) 1px solid; margin: 0px 10px 10px; padding-left: 0px; float: right; clear: both; border-top: rgb(255,255,255) 1px solid; border-right: rgb(255,255,255) 1px solid" src="http://i3.6.cn/cvbnm/06/e9/0d/fe543fab025def9627a7f1a4df30f354.jpg" /> MTracer2.0采用注册号的方式注册。看来作者深知在国内破解成风，于是很幽默地添加了这样一则菜单选项：如何8小时破解本软件？呵呵。8小时我们可以做许多事情，没有必要浪费在track、debug、crack上。你的时间很值钱的。</p>
<p>这两款软件的完全版我这里都有，如果需要RegexBuddy的话，请占击链接留下信箱索取即可；但是MTracer的话就<strong>不要</strong>索取了。</p>
<p>&#160;</p>
<h2>相关阅读</h2>
<h3><a href="http://iregex.org/blog/regexbuddy.html">windows下的正则式工具介绍之一：RegexBuddy</a></h4>
<h3><a href="http://iregex.org/blog/powergrep.html">windows下的正则式工具介绍之二：powergrep</a></h4>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/mtracer-vs-regexbuddy.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>探索匹配中文的正则表达式</title>
		<link>http://iregex.org/blog/exploration-on-regular-rexpressions-that-match-chinese.html</link>
		<comments>http://iregex.org/blog/exploration-on-regular-rexpressions-that-match-chinese.html#comments</comments>
		<pubDate>Sat, 23 Aug 2008 16:22:29 +0000</pubDate>
		<dc:creator>rex</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[chinese]]></category>

		<category><![CDATA[regex]]></category>

		<category><![CDATA[unicode]]></category>

		<category><![CDATA[utf8]]></category>

		<category><![CDATA[正则表达式]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=31</guid>
		<description><![CDATA[
按：本文使用的RegexBuddy为3.1.0（完全）版，并非最新版3.1.1（截至2008.08.23）。需要该版本的请在这篇文章后留言。
注：参考www.regular-expressions.info的风格，更新了本模板的style.css文件，加入了与正则式代码相关的格式： 

正则式格式举例：[a-z]+@[a-z]+?\.[a-z]+ 
匹配格式举例：pig@animals.com和chicken@birds.com 
普通文本格式举例：这是一些普通文本。hello regex world. pig@animals.com和chicken@birds.com 

可以这样使用：在字符串这是一些普通文本。hello regex world. pig@animals.com和chicken@birds.com使用正则式[a-z]+@[a-z]+?\.[a-z]+加以匹配，得到的结果为：pig@animals.com和chicken@birds.com。 

极端粗放型：点号其实是近乎万能的，可以匹配任何字符，限制只在于换行符的匹配上。匹配中文自然不在话下。作为可有可无的背景符，一个.*就能匹配掉包括中文在内的全部字符。这当然是一种极端的情况，因为这样显示不出中文字符串的特性。这不是本文要探讨的。
极端集约型：如果搜索特定文本，例如在一二三四五六七八九十拾佰百千仟万亿中匹配十拾， 直接使用m/十拾/就能搞定。这同样不是本文要探讨的。与\w能匹配英文字母一样，本文想找的是能够匹配所有汉字，而不匹配其它文本的一种简写方式。 
普适型型：由于汉字属于Unicode，我们就从unicode里面找。在Unicode Regular Expressions，列出了unicode的许多种表达方式。搜索chinese，找到如下一行：



Writing Systems
Blocks


&#8230;
&#8230;


Chinese
CJK Unified Ideographs, CJK Unified Ideographs Extension A, CJK Compatibility Ideographs, CJK Compatibility Forms, Enclosed CJK Letters and Months, Small Form Variants, Bopomofo, Bopomofo Extended



关于CJK的含义，是指中日韩统一表意文字（Chinese Japanese Korean Unified Ideographs），可以参考百度释义，或wiki词条。
再查了一下regular expressions,查到其unicode一节有这样的内容：

\p{InCJK_Unified_Ideographs}: U+4E00..U+9FFF 

看到这里，我想起了以前写的《匹配用户名的asp正则表达式(包括中文)》一文中，提到的中文匹配为[\u4e00-\u9fa5]，原来是有其对应的速记方式的，虽然两者有最后一组字符的差异。看附图可见U+9fa5，最后一个汉字的模样。 此序列的第一位，U+4e00，是汉字一。

自定义：到目前为止，相当于给汉字找到了官方的身份和说法，使用\p{InCJK_Unified_Ideographs}就能匹配所有的中文字符。我们其实也可以将一些重复出现的东西，封装起来，以备使用。例如，对于阿拉伯数字，我们有\d可以用。对于中文数字一二三四等等，我们有没有办法呢？

?View Code PERL$zh_digit=qr/一&#124;二&#124;三&#124;四&#124;五&#124;六&#124;七&#124;八&#124;九&#124;十&#124;零&#124;〇&#124;百&#124;千&#124;万&#124;亿&#124;佰&#124;仟&#124;壹&#124;贰&#124;叁&#124;肆&#124;伍&#124;陆&#124;柒&#124;捌&#124;玖&#124;拾/;
&#160;
$str=&#34;人民币五十一万零三百元整。大写：伍拾壹万零三佰元整。&#34;;
while&#40;$str =~ s/&#40;&#40;?:$zh_digit&#41;+&#41;//&#41;
&#123;
	print $1.&#34;\n&#34;;
&#125;





其输出结果见附图。
结论
可以使用\p{InCJK_Unified_Ideographs}匹配任意中文字符。在不支持该种标记方式时，也可以使用[\u4e00-\u9fa5]加以匹配。
关于文正则表达式，我觉得尚未穷其奥秘。以前在linux（utf8编码）下，编写scim输入平台的郑码码表时，匹配中文所使用的正则表达式为[\x80-\xff]{3}，也能很好地工作。请参阅此文：龙文郑码码表 for [...]]]></description>
			<content:encoded><![CDATA[<blockquote>
<p>按：本文使用的RegexBuddy为3.1.0（完全）版，并非最新版3.1.1（截至2008.08.23）。需要该版本的请在<a href="http://iregex.org/blog/regexbuddy.html" target="_blank"><font color="#ff008c">这篇</font></a>文章后留言。</p>
<p>注：参考<a href="http://www.regular-expressions.info" target="_blank">www.regular-expressions.info</a>的风格，更新了本模板的style.css文件，加入了与正则式代码相关的格式： </p>
<ul>
<li><strong>正则式</strong>格式举例：<tt class="regex">[a-z]+@[a-z]+?\.[a-z]+</tt> </li>
<li><strong>匹配</strong>格式举例：<tt class="match">pig@animals.com</tt>和<tt class="match">chicken@birds.com</tt> </li>
<li><strong>普通文本</strong>格式举例：<tt class="string">这是一些普通文本。hello regex world. pig@animals.com和chicken@birds.com</tt> </li>
</ul>
<p>可以这样使用：在字符串<tt class="string">这是一些普通文本。hello regex world. pig@animals.com和chicken@birds.com</tt>使用正则式<tt class="regex">[a-z]+@[a-z]+?\.[a-z]+</tt>加以匹配，得到的结果为：<tt class="match">pig@animals.com</tt>和<tt class="match">chicken@birds.com</tt>。 </p>
</blockquote>
<p><strong>极端粗放型</strong>：点号其实是近乎万能的，可以匹配任何字符，限制只在于换行符的匹配上。匹配中文自然不在话下。作为可有可无的背景符，一个<tt class="regex">.*</tt>就能匹配掉包括中文在内的全部字符。这当然是一种极端的情况，因为这样显示不出中文字符串的特性。这不是本文要探讨的。</p>
<p><strong>极端集约型</strong>：如果搜索特定文本，例如在<tt class="string">一二三四五六七八九十拾佰百千仟万亿</tt>中匹配<tt class="regex">十拾</tt>， 直接使用m/<tt class="regex">十拾</tt>/就能搞定。这同样不是本文要探讨的。与<tt class="regex">\w</tt>能匹配英文字母一样，本文想找的是能够匹配所有汉字，而不匹配其它文本的一种简写方式。 </p>
<p><strong>普适型型</strong>：由于汉字属于Unicode，我们就从unicode里面找。在<a href="http://unicode.org/reports/tr18/" target="_blank">Unicode Regular Expressions</a>，列出了unicode的许多种表达方式。搜索chinese，找到如下一行：</p>
<table width="400" border="1" cellpadding="2" cellspacing="1" unselectable="on">
<tbody>
<tr>
<td  valign="top" width="200">Writing Systems</td>
<td  valign="top" width="200">Blocks</td>
</tr>
<tr>
<td  valign="top" width="200">&#8230;</td>
<td  valign="top" width="200">&#8230;</td>
</tr>
<tr>
<td  valign="top" width="200">Chinese</td>
<td  valign="top" width="200">CJK Unified Ideographs, CJK Unified Ideographs Extension A, CJK Compatibility Ideographs, CJK Compatibility Forms, Enclosed CJK Letters and Months, Small Form Variants, Bopomofo, Bopomofo Extended</td>
</tr>
</tbody>
</table>
<p>关于CJK的含义，是指中日韩统一表意文字（Chinese Japanese Korean Unified Ideographs），可以参考<a href="http://baike.baidu.com/view/628156.html" target="_blank">百度释义</a>，或<a href="http://en.wikipedia.org/wiki/CJK" target="_blank">wiki</a>词条。</p>
<p>再查了一下<a href="http://www.regular-expressions.info/" target="_blank">regular expressions</a>,查到其<a href="http://www.regular-expressions.info/unicode.html" target="_blank">unicode</a>一节有这样的内容：
</p>
<blockquote><p><tt class="regex">\p{InCJK_Unified_Ideographs}</tt>: U+4E00..U+9FFF </p></blockquote>
<p>
看到这里，我想起了以前写的<a href="http://iregex.org/blog/regular-expressions-to-match-chinese-username-in-asp.html" target="_blank">《匹配用户名的asp正则表达式(包括中文)》</a>一文中，提到的中文匹配为<tt class="regex">[\u4e00-\u9fa5]</tt>，原来是有其对应的速记方式的，虽然两者有最后一组字符的差异。看附图可见U+9fa5，最后一个汉字的模样。<img src="http://i3.6.cn/cvbnm/80/3c/69/ac41d1186fde1c67bf7cef334bc6a0c7.jpg" style="border: 1px solid rgb(255, 255, 255); margin: 0px 10px 10px; clear: both; padding-left: 0px; " alt="我爱正则表达式｜在RegexBuddy中如何使用正则表达式匹配中文字符｜http://iregex.org" /> 此序列的第一位，U+4e00，是汉字<tt class="string">一</tt>。
</p>
<p><strong>自定义</strong>：到目前为止，相当于给汉字找到了官方的身份和说法，使用<tt class="regex">\p{InCJK_Unified_Ideographs}</tt>就能匹配所有的中文字符。我们其实也可以将一些重复出现的东西，封装起来，以备使用。例如，对于阿拉伯数字，我们有<tt class="regex">\d</tt>可以用。对于中文数字一二三四等等，我们有没有办法呢？</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p31code18'); return false;">View Code</a> PERL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p3118"><td class="code" id="p31code18"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">$zh_digit</span><span style="color: #339933;">=</span><span style="color: #009966; font-style: italic;">qr/一|二|三|四|五|六|七|八|九|十|零|〇|百|千|万|亿|佰|仟|壹|贰|叁|肆|伍|陆|柒|捌|玖|拾/</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$str</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;人民币五十一万零三百元整。大写：伍拾壹万零三佰元整。&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$str</span> <span style="color: #339933;">=~</span> <span style="color: #000066;">s</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?:</span><span style="color: #0000ff;">$zh_digit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">//</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">print</span> <span style="color: #0000ff;">$1</span>.<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p>
<img src="http://i3.6.cn/cvbnm/6f/3d/c2/f974a15dbf6a2ceed6c6744961f39b27.jpg" src="http://i3.6.cn/cvbnm/80/3c/69/ac41d1186fde1c67bf7cef334bc6a0c7.jpg" style="border: 1px solid rgb(255, 255, 255); margin: 0px 10px 10px; clear: both; padding-left: 0px; " alt="我爱正则表达式｜在RegexBuddy中如何使用正则表达式匹配中文字符｜http://iregex.org" />
</p>
<p>其输出结果见附图。</p>
<h3>结论</h3>
<p>可以使用<tt class="regex">\p{InCJK_Unified_Ideographs}</tt>匹配任意中文字符。在不支持该种标记方式时，也可以使用<tt class="regex">[\u4e00-\u9fa5]</tt>加以匹配。</p>
<p>关于文正则表达式，我觉得尚未穷其奥秘。以前在linux（utf8编码）下，编写scim输入平台的郑码码表时，匹配中文所使用的正则表达式为<tt class="regex">[\x80-\xff]{3}</tt>，也能很好地工作。请参阅此文：<a href="http://zhasm.com/blog/longwen-zhengma-ime-table-in-scim-format.html" target="_blank" title="我爱正则表达式｜在RegexBuddy中如何使用正则表达式匹配中文字符｜http://iregex.org">龙文郑码码表 for scim</a>。其原理我尚不清楚，留待之后有时间研究。如有知情者，也请不吝赐教，先行谢过。</p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/exploration-on-regular-rexpressions-that-match-chinese.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>《精通正则表达式》视频教程提供下载</title>
		<link>http://iregex.org/blog/mastering-regular-expressions-vedio-tutorial-download.html</link>
		<comments>http://iregex.org/blog/mastering-regular-expressions-vedio-tutorial-download.html#comments</comments>
		<pubDate>Sun, 17 Aug 2008 04:02:35 +0000</pubDate>
		<dc:creator>rex</dc:creator>
		
		<category><![CDATA[tutorial]]></category>

		<category><![CDATA[newbie]]></category>

		<category><![CDATA[regex]]></category>

		<category><![CDATA[正则式]]></category>

		<category><![CDATA[正则表达式]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=28</guid>
		<description><![CDATA[偶然从网上找到该教程，下载后觉得不错，可以作为《精通正则表达式》的番外篇，共同学习。
关于此视频的讲师：
此视频的讲师为余晟先生。余先生是抓虾网高级顾问。毕业于东北师范大学，主修计算机，辅修中文。现居北京。曾任高级程序员、技术经理；从事过大量文本解析和数据抽取的工作。对程序语言、算法、数据库和敏捷开发都有兴趣，译有《精通正则表达式》(第3版)一书。
关于此视频
此视频分为5讲，每讲30分钟左右，内容深入浅出，适合以下受众：

对正则式感兴趣的人；
对正则式不感兴趣的人；
正则式初学者，想入门；
正则式有所成者，想提高。

当然，如果能静下心来，通读《精通正则表达式》原书，并亲自动手尝试，效果更为显著。
目录及下载：
视频文件（avi格式）已经使用7-zip压缩，使其总尺寸从784Mb减小到74.7Mb。您需要使用支持7-zip的解压软件才能打开。
上传空间在mediafire。在网络封锁日益严峻的大环境下，您或许需要使用代那个理才能访问。我不敢保证此文件址长时有效。需要下载的请抓紧时间。



章节
内容
大小（Mb）
链接


第一讲

15.5
点此下载


第二讲

13.1
点此下载


第三讲

16.6
点此下载


第四讲

15.3
点此下载


第五讲

13.9
点此下载


源代码

16.9Kb
点此下载



2008.11.04更新：
由于mediafire不出意料被AND（不幸言中），上面的文件也无法下载。好在还有纳米盘。请访问牛腩的学习空间去使用纳米盘下载。上面除了我这里提到的5部分《精通正则表达式》vedio以外，还有两段“实战正则表达式”，还不赶紧去看看？
]]></description>
			<content:encoded><![CDATA[<p>偶然从<a href="http://tieba.baidu.com/f?kz=464065073" target="_blank">网上</a>找到该教程，下载后觉得不错，可以作为《<a href="http://www.douban.com/subject/2154713/" target="_blank">精通正则表达式</a>》的番外篇，共同学习。</p>
<h3>关于此视频的讲师：</h3>
<p>此视频的讲师为<a href="http://www.luanxiang.org/blog/" target="_blank">余晟</a>先生。余先生是抓虾网高级顾问。毕业于东北师范大学，主修计算机，辅修中文。现居北京。曾任高级程序员、技术经理；从事过大量文本解析和数据抽取的工作。对程序语言、算法、数据库和敏捷开发都有兴趣，译有《精通正则表达式》(第3版)一书。</p>
<h3>关于此视频</h3>
<p>此视频分为5讲，每讲30分钟左右，内容深入浅出，适合以下受众：</p>
<ul>
<li>对正则式感兴趣的人；</li>
<li>对正则式不感兴趣的人；</li>
<li>正则式初学者，想入门；</li>
<li>正则式有所成者，想提高。</li>
</ul>
<p>当然，如果能静下心来，通读《<a href="http://www.douban.com/subject/2154713/" target="_blank">精通正则表达式</a>》原书，并亲自动手尝试，效果更为显著。</p>
<h3>目录及下载：</h3>
<p>视频文件（avi格式）已经使用<a href="http://www.7-zip.org/" target="_blank">7-zip</a>压缩，使其总尺寸从784Mb减小到74.7Mb。您需要使用支持7-zip的解压软件才能打开。</p>
<p>上传空间在<a href="http://www.mediafire.com" target="_blank">mediafire</a>。在网络封锁日益严峻的大环境下，您或许需要使用代那个理才能访问。我不敢保证此文件址长时有效。需要下载的请抓紧时间。</p>
<table border="1" cellspacing="1" cellpadding="2" width="385">
<tbody>
<tr>
<td width="43" align="middle">章节</td>
<td width="254" align="middle" valign="top">内容</td>
<td width="42" align="middle">大小（Mb）</td>
<td width="39" align="middle">链接</td>
</tr>
<tr>
<td width="46" align="middle">第一讲</td>
<td width="256" valign="top"><a href="http://tu.6.cn/pic/show/id/353660"><img src="http://i3.6.cn/cvbnm/38/97/2e/af092f3b004368b914f828e87bf0a700.jpg" alt="" /></a></td>
<td width="44" align="middle">15.5</td>
<td width="41" align="middle"><a title="我爱正则表达式｜精通正则式视频教程｜资源下载" href="http://www.mediafire.com/file/zgdlmjrryn1" target="_blank"><del datetime="2008-11-04T13:59:50+00:00">点此下载</del></a></td>
</tr>
<tr>
<td width="47" align="middle">第二讲</td>
<td width="256" valign="top"><a href="http://tu.6.cn/pic/show/id/353661"><img src="http://i3.6.cn/cvbnm/6b/e9/a0/6ed09224a2cf26678f5b12b4f5279178.jpg" alt="" /></a></td>
<td width="45" align="middle">13.1</td>
<td width="42" align="middle"><a title="我爱正则表达式｜精通正则式视频教程｜资源下载" href="http://www.mediafire.com/file/rhjhdvzjrna" target="_blank"><del datetime="2008-11-04T13:59:50+00:00">点此下载</del></a></td>
</tr>
<tr>
<td width="46" align="middle">第三讲</td>
<td width="256" valign="top"><a href="http://tu.6.cn/pic/show/id/353662"><img src="http://i3.6.cn/cvbnm/a2/c2/b4/92dd6a034a2c806929956c85f55bb182.jpg" alt="" /></a></td>
<td width="45" align="middle">16.6</td>
<td width="43" align="middle"><a title="我爱正则表达式｜精通正则式视频教程｜资源下载" href="http://www.mediafire.com/file/uhsktmtle6u" target="_blank"><del datetime="2008-11-04T13:59:50+00:00">点此下载</del></a></td>
</tr>
<tr>
<td width="46" align="middle">第四讲</td>
<td width="256" valign="top"><a href="http://tu.6.cn/pic/show/id/353663"><img src="http://i3.6.cn/cvbnm/3e/d7/f6/fcc2d994ed9ae134c8cf6d51a0beaeff.jpg" alt="" /></a></td>
<td width="45" align="middle">15.3</td>
<td width="43" align="middle"><a title="我爱正则表达式｜精通正则式视频教程｜资源下载" href="http://www.mediafire.com/file/8inm2eqwnrk" target="_blank"><del datetime="2008-11-04T13:59:50+00:00">点此下载</del></a></td>
</tr>
<tr>
<td width="46" align="middle">第五讲</td>
<td width="256" valign="top"><a href="http://tu.6.cn/pic/show/id/353664"><img src="http://i3.6.cn/cvbnm/21/48/84/f48278bc22bb44e4db5e036bece56436.jpg" alt="" /></a></td>
<td width="45" align="middle">13.9</td>
<td width="43" align="middle"><a title="我爱正则表达式｜精通正则式视频教程｜资源下载" href="http://www.mediafire.com/file/mphmtaojmlh" target="_blank"><del datetime="2008-11-04T13:59:50+00:00">点此下载</del></a></td>
</tr>
<tr>
<td width="46" align="middle">源代码</td>
<td width="256" valign="top"></td>
<td width="45" align="middle">16.9Kb</td>
<td width="43" align="middle"><a href="http://www.mediafire.com/?bzmntyv3z5z" target="_blank"><del datetime="2008-11-04T13:59:50+00:00">点此下载</del></a></td>
</tr>
</tbody>
</table>
<p><strong><font color="#FF008c">2008.11.04更新：</font></strong><br />
由于mediafire不出意料被AND（不幸言中），上面的文件也无法下载。好在还有<a href="http://www.namipan.com">纳米盘</a>。请访问<a href="http://hi.baidu.com/niunan8/blog/item/706825c2eef38533e5dd3bb4.html"><font color="#33ff33">牛腩的学习空间</font></a>去使用纳米盘下载。上面除了我这里提到的5部分《精通正则表达式》vedio以外，还有两段“实战正则表达式”，还不赶紧去看看？</p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/mastering-regular-expressions-vedio-tutorial-download.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>提供《正则表达式袖珍参考书》英文第二版pdf下载</title>
		<link>http://iregex.org/blog/regular-expression-pocket-reference.html</link>
		<comments>http://iregex.org/blog/regular-expression-pocket-reference.html#comments</comments>
		<pubDate>Fri, 08 Aug 2008 02:36:43 +0000</pubDate>
		<dc:creator>rex</dc:creator>
		
		<category><![CDATA[resources]]></category>

		<category><![CDATA[ebook]]></category>

		<category><![CDATA[languages]]></category>

		<category><![CDATA[pdf]]></category>

		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=26</guid>
		<description><![CDATA[
无论对于正则式初学者还是老手，如果想从头至尾了解各种正则式流派之差异，深究正则式之匹配原理，打磨高效率的正则式，《精通正则表达式》(豆瓣链接)都是不二之选。但是该书长处在于推理、演绎，归纳倒在其次，虽然也有不少表格，但是作者一再强调，不要把本书仅仅当作参考手册来读。因为正则式参考手册另有其书：《OReilly.Regular.Expression.Pocket.Reference》(豆瓣链接)。
该书与《精通正则表达式》同出自O&#8217;Reilly，第二版发行于2007年。我找到了pdf版，1M左右，排版精良。涵盖的语言有：

Perl 5.8,
Java(java.util.regex),
.NET and C#,
 PHP,
Python,
Ruby,
 JavaScript,
 PCRE,
Apache Web Server,
 vi Editor, Shell Tools。

上述每一种语言/工具都分别单独有一章来详细介绍其正则特性。相比之下，《精通正则表达式》中只有Perl、PHP、Java、.Net有此殊荣。
阅读建议：

《精通正则表达式》有情节有起伏，有推理有结论，有感悟有启发，可以当作小说，从头读到尾（至少前6章如此）。
《正则表达式袖珍参考书》就是一本迷你的正则式百科全书。可以当字典、当语法书来读。

该书128页，一本小册子而已。售价US $14.99，或CAN $17.99。如果您囊中不羞涩，强烈建议你购买正版。如果您只是抱着纯学术目的钻研一下正则技术，可以先从这里下载电子版，记得及时删除哦。以下地址必有一款适合您；有的或许需要代理才能访问。如果使用代理也无法访问时，可以留言，我会把pdf发到您的邮箱。



Hosting
URL


Fs2You
click here


MediaFire
click here


SkyDrivev
click here



]]></description>
			<content:encoded><![CDATA[<p><img style="border: 1px solid #ffffff; margin: 0px 10px 10px; clear: both; padding-left: 0px; float: right;" src="http://i3.6.cn/cvbnm/8d/98/7a/4b3496857a4ae8ed58d4b4c9b989b19b.jpg" alt="我爱正则表达式｜《精通正则表达式》英文第三版PDF提供下载" width="200" /><br />
无论对于正则式初学者还是老手，如果想从头至尾了解各种正则式流派之差异，深究正则式之匹配原理，打磨高效率的正则式，《<a title="我爱正则表达式｜《精通正则表达式》英文第三版PDF提供下载" href="http://iregex.org/blog/mastering-regular-expressions-3rd-pdf-ebook-download.html" target="_blank">精通正则表达式</a>》(<a title="我爱正则表达式｜《精通正则表达式》|豆瓣" href="http://www.douban.com/subject/2154713/" target="_blank">豆瓣链接</a>)都是不二之选。但是该书长处在于推理、演绎，归纳倒在其次，虽然也有不少表格，但是作者一再强调，不要把本书仅仅当作参考手册来读。因为正则式参考手册另有其书：《OReilly.Regular.Expression.Pocket.Reference》(<a title="我爱正则表达式｜《正则表达式袖珍参考书》｜豆瓣" href="http://www.douban.com/subject/2363803/" target="_blank">豆瓣链接</a>)。</p>
<p>该书与《<a title="我爱正则表达式｜《精通正则表达式》英文第三版PDF提供下载" href="http://iregex.org/blog/mastering-regular-expressions-3rd-pdf-ebook-download.html" target="_blank">精通正则表达式</a>》同出自O&#8217;Reilly，第二版发行于2007年。我找到了pdf版，1M左右，排版精良。涵盖的语言有：</p>
<ul>
<li>Perl 5.8,</li>
<li>Java(java.util.regex),</li>
<li>.NET and C#,</li>
<li> PHP,</li>
<li>Python,</li>
<li>Ruby,</li>
<li> JavaScript,</li>
<li> PCRE,</li>
<li>Apache Web Server,</li>
<li> vi Editor, Shell Tools。</li>
</ul>
<p>上述每一种语言/工具都分别单独有一章来详细介绍其正则特性。相比之下，《<a title="我爱正则表达式｜《精通正则表达式》英文第三版PDF提供下载" href="http://iregex.org/blog/mastering-regular-expressions-3rd-pdf-ebook-download.html" target="_blank">精通正则表达式</a>》中只有Perl、PHP、Java、.Net有此殊荣。</p>
<p>阅读建议：</p>
<ul>
<li>《<a title="我爱正则表达式｜《精通正则表达式》英文第三版PDF提供下载" href="http://iregex.org/blog/mastering-regular-expressions-3rd-pdf-ebook-download.html" target="_blank">精通正则表达式</a>》有情节有起伏，有推理有结论，有感悟有启发，可以当作小说，从头读到尾（至少前6章如此）。</li>
<li>《正则表达式袖珍参考书》就是一本迷你的正则式百科全书。可以当字典、当语法书来读。</li>
</ul>
<p>该书128页，一本小册子而已。售价US $14.99，或CAN $17.99。如果您囊中不羞涩，强烈建议你购买正版。如果您只是抱着纯学术目的钻研一下正则技术，可以先从这里下载电子版，记得及时删除哦。以下地址必有一款适合您；有的或许需要代理才能访问。如果使用代理也无法访问时，可以留言，我会把pdf发到您的邮箱。</p>
<table border="1" cellspacing="1" cellpadding="2" width="400">
<tbody>
<tr>
<td width="200" valign="top">Hosting</td>
<td width="200" valign="top">URL</td>
</tr>
<tr>
<td width="200" valign="top">Fs2You</td>
<td width="200" valign="top"><a title="我爱正则表达式｜《正则表达式袖珍参考书》" href="http://www.fs2you.com/files/2b809a7d-645c-11dd-959e-0014221b798a/" target="_blank">click here</a></td>
</tr>
<tr>
<td width="200" valign="top">MediaFire</td>
<td width="200" valign="top"><a title="我爱正则表达式｜《正则表达式袖珍参考书》" href="http://www.mediafire.com/?ymzn2mtupiq" target="_blank">click here</a></td>
</tr>
<tr>
<td width="200" valign="top">SkyDrivev</td>
<td width="200" valign="top"><a title="我爱正则表达式｜《正则表达式袖珍参考书》" href="http://cid-b6efba48a7f83d5b.skydrive.live.com" target="_blank">click here</a></td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/regular-expression-pocket-reference.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>如狐添翼：FireFox3正则表达式搜索插件/Find Bar/</title>
		<link>http://iregex.org/blog/find-bar-for-firefox-and-thunderbird-as-regular-expression-searching-engine.html</link>
		<comments>http://iregex.org/blog/find-bar-for-firefox-and-thunderbird-as-regular-expression-searching-engine.html#comments</comments>
		<pubDate>Mon, 28 Jul 2008 02:18:57 +0000</pubDate>
		<dc:creator>rex</dc:creator>
		
		<category><![CDATA[工具软件]]></category>

		<category><![CDATA[firefox]]></category>

		<category><![CDATA[plugin]]></category>

		<category><![CDATA[thunderbird]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=20</guid>
		<description><![CDATA[原来在FireFox2.x中使用过一个插件FindBar，十分好用。升级到firefox3.0之后，FB居然冬眠。今天终于看到更新，能继续在firefox中使用了，心情超爽，特写日志以记之。
意译一下作者的介绍。插件主页、介绍原文在这里。
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-正则表达式分割线&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
/Find Bar/
/Find Bar/ is a new extension for Firefox that is still a little bit in progress. I&#8217;ve always believed that one of the best features of Firefox is it&#8217;s quick find bar. I probably use it about 20 times a day, if not more. But it has to be said its a fairly simple [...]]]></description>
			<content:encoded><![CDATA[<p>原来在FireFox2.x中使用过一个插件FindBar，十分好用。升级到firefox3.0之后，FB居然冬眠。今天终于看到更新，能继续在firefox中使用了，心情超爽，特写日志以记之。<a href="https://1n5vfq.bay.livefilestore.com/y1p9yjXzii5NFmllV2lT0g4PYDrg90_Z4F1mRJBxymy-0BQ4AB-Maiua3F9ktYIVYLWP4yd_EqbB_mMUAq_6zKc7g/%E5%8F%A4%E5%BA%99%E5%87%84%E9%A3%8E.mp3"></a></p>
<p>意译一下作者的介绍。插件主页、介绍原文在<a href="http://www.oxymoronical.com/web/firefox/FindBarRX" target="_blank">这里</a>。</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<a href="http://iregex.org" target="_blank">正则表达式</a>分割线&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<h3>/Find Bar/</h3>
<p>/Find Bar/ is a new extension for Firefox that is still a little bit in progress. I&#8217;ve always believed that one of the best features of Firefox is it&#8217;s quick find bar. I probably use it about 20 times a day, if not more. But it has to be said its a fairly simple beast. When it comes to more powerful searches you&#8217;re just out of luck. This extension adds a whole new dimension to the find bar, regular expressions. The regular expressions are implemented using the JavaScript engine so check the JavaScript RegExp syntax for the full details.</p>
<p>/Find Bar/是FireFox的新插件，目前仍在完善中。对于firefox，我一直觉得它方便快捷的搜索栏是其亮点之一。我每天使用该功能20次以上。但是必须承认，其搜索功能太过薄弱。当你要搜索更复杂的内容时，它就无能为力了。</p>
<p>本插件为搜索栏添加了新选项：<a href="http://iregex.org" target="_blank">正则表达式</a>。它使用了JavaScript正则式引擎。请查询<a href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Regular_Expressions" target="_blank">JavaScript RegExp文档</a>来了解语法细节内容。</p>
<p>有待解决的两个问题：</p>
<ul>
<li> Whitespace. HTML is made up a lot of this, most of it is ignored by the browser and not visible on the page. At the moment this extension doesn’t ignore anything so you may find there are more spaces between words than you expected. Should I err on the side of accuracy as it is now, or collapse all whitespace?空白符（水平制表符、空格等）。HTML中包含了许多空白符，其中一大部分被浏览器忽略掉，不在页面上显示出来。目前该插件没有忽略任何字符，因此你会发现，单词之间往往有更多的空白符符。我是该精确地显示出每个空白字符呢，还是该将其全部压缩？</li>
<li> Block content. The standard find bar wont find searches that span blocks (paragraphs in human terms). This extension does, which while potentially useful also causes some issues. One thought is to make each paragraph like a line then you can use line breaks to match paragraphs as you might expect.区块内容。标准的搜索栏不会跨区块（亦即段落）搜索，而本插件搜索时却会跨越区块，这既是便利之处，但是也有可能带来副作用。一种思路是将每一段落处理为一个文本行，这样您就可以使用换行符来匹配段落。</li>
</ul>
<h3>该插件的兼容性</h3>
<p style="text-align: center; font-weight: bold; font-size: 107%;">
<table border="1" cellspacing="1" cellpadding="2" width="281">
<tbody>
<tr>
<td width="101" valign="top">FireFox</td>
<td width="52" valign="top"><img style="vertical-align: middle;" title="Firefox" src="http://www.oxymoronical.com/shared/images/firefox.png" alt="Firefox" /></td>
<td width="122" valign="top">2.0b1 - 3.1a2pre</td>
</tr>
<tr>
<td width="103" valign="top">ThunderBird</td>
<td width="54" valign="top"><img style="vertical-align: middle;" title="Thunderbird" src="http://www.oxymoronical.com/shared/images/thunderbird.png" alt="Thunderbird" /></td>
<td width="122" valign="top">2.0 - 3.0a1</td>
</tr>
</tbody>
</table>
<h3>下载地址</h3>
<p style="text-align: center; font-weight: bold; font-size: 107%;"><a href="https://addons.mozilla.org/downloads/file/34434/_find_bar_-1.0.1-fx+tb.xpi"><img style="vertical-align: middle;" src="https://addons.mozilla.org/img/addon-icn.png" alt="/Find Bar/" /></a> <a href="https://addons.mozilla.org/downloads/file/34434/_find_bar_-1.0.1-fx+tb.xpi">Install v1.0.1</a></p>
<p style="text-align: center; font-size: 84%;">(Installs from <a href="https://addons.mozilla.org/addon/6534">addons.mozilla.org</a>)</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<a href="http://iregex.org" target="_blank">正则表达式</a>分割线&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>跨区块搜索我觉得不是大问题，对于空白字符，我的解决方法是，只要是使用<a href="http://iregex.org" target="_blank">正则表达式</a>来搜索，就在使用空格时使用\s+来表示。这样，从内容上，它表示了任何种类的空白字符；从数量上，它表示了最少一个（多则不限）的空白字符，既不错杀，也无冤死。</p>
<p>由此引发的感慨是：人的眼睛所能看到的字符是有限的，其所看到内容与事实的真相有时未必相符。而程序是精确的，每一个比特都明察秋毫。（例如在汇编语言中的数字0与ASCII字符&#8217;0&#8242;就截然不同。虽然在perl中，两者单独出现时被视为是同一事物。）</p>
<p>该插件还能结合原搜索栏的全部高亮显示选项来显示，便于显示所有的匹配。在搜索英文等外语时，你会发现这一点十分有用。</p>
<p>另外有一点得陇望蜀的心思是，如果该插件支持保存常用搜索就好了。比方搜索email地址的、URL的，日期的，诸如此类。虽如此，这款插件已经十分好用，强烈推荐。</p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/find-bar-for-firefox-and-thunderbird-as-regular-expression-searching-engine.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>匹配用户名的asp正则表达式(包括中文)</title>
		<link>http://iregex.org/blog/regular-expressions-to-match-chinese-username-in-asp.html</link>
		<comments>http://iregex.org/blog/regular-expressions-to-match-chinese-username-in-asp.html#comments</comments>
		<pubDate>Sun, 13 Jul 2008 14:01:17 +0000</pubDate>
		<dc:creator>rex</dc:creator>
		
		<category><![CDATA[Q&amp;A]]></category>

		<category><![CDATA[asp]]></category>

		<category><![CDATA[chinese]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=18</guid>
		<description><![CDATA[有人在正则表达式中文站贴出这样一道问题：

求ASP 用户名 表达式
用户名长度在2-20字符之间，由中文/大小写字母/数字/中划线-/下线线_组成。
这个问题不算难，只要下边一行核心代码就能搞定：

?View Code PERL&#34;^[-_a-zA-Z0-9\u4e00-\u9fa5]{2,20}$&#34;

关键是没有使用过ASP语言。按此页的提示，设置了ASP环境。查询了一些在线的入门级ASP教程之后，解答如下：

?View Code ASP&#160;
&#60;form action=&#34;verify.asp&#34; method=&#34;post&#34;&#62;
姓名：
&#60;input name=&#34;name&#34; type=&#34;text&#34; /&#62;
&#160;
&#60;input name=&#34;Submit&#34; type=&#34;submit&#34; value=&#34;提交&#34; /&#62;
&#60;input name=&#34;Submit2&#34; type=&#34;reset&#34; value=&#34;重置&#34; /&#62;
&#60;/form&#62;

它调用以下verify.asp文件：

?View Code ASP&#38;lt;%
Function RegExpTest&#40;patrn, strng&#41;
Dim regEx, retVal ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = False ' 设置是否区分大小写。
retVal = regEx.Test&#40;strng&#41; ' 执行搜索测试。
If retVal Then
RegExpTest = &#34;合法用户名。&#34;
Else
RegExpTest = &#34;非法用户名。&#34;
End If
End Function
&#160;
name=request.form&#40;&#34;name&#34;&#41;
psw=request.form&#40;&#34;psw&#34;&#41;
sex=request.form&#40;&#34;sex&#34;&#41;
city=request.form&#40;&#34;city&#34;&#41;
Response.write RegExpTest&#40;&#34;^[-_a-zA-Z0-9\u4e00-\u9fa5]{2,20}$&#34;, [...]]]></description>
			<content:encoded><![CDATA[<p>有人在<a href="http://www.regex.net.cn" target="_blank">正则表达式中文站</a>贴出<a href="http://www.regex.net.cn/redirect.php?tid=30&amp;goto=lastpost#lastpost" target="_blank">这样</a>一道问题：</p>
<blockquote>
<h3>求