<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>我爱正则表达式 &#187; 杂项</title>
	<atom:link href="http://iregex.org/blog/category/miscellaneous/feed" rel="self" type="application/rss+xml" />
	<link>http://iregex.org</link>
	<description>原创、翻译、转载关于正则表达式的文章</description>
	<lastBuildDate>Sun, 27 Jun 2010 04:20:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/><atom:link rel="hub" href="http://www.feedsky.com/api/RPC2"/><atom:link rel="hub" href="http://blogsearch.google.com/ping/RPC2"/><atom:link rel="hub" href="http://blog.yodao.com/ping/RPC2"/><atom:link rel="hub" href="http://www.feedsky.com/api/RPC2"/><atom:link rel="hub" href="http://www.xianguo.com/xmlrpc/ping.php"/><atom:link rel="hub" href="http://www.zhuaxia.com/rpc/server.php"/><atom:link rel="hub" href="http://rpc.technorati.com/rpc/ping"/><atom:link rel="hub" href="http://rpc.pingomatic.com/"/>	
<!-- Start Of Script Generated By WP-PostViews Plus -->
<script type='text/javascript' src='http://iregex.org/wp-includes/js/jquery/jquery.js?ver=1.4.2'></script>
<script type="text/javascript">
/* <![CDATA[ */
/* ]]> */
</script>
<!-- End Of Script Generated By WP-PostViews Plus -->
	<item>
		<title>正则笔记</title>
		<link>http://iregex.org/blog/regex-note-20100621.html</link>
		<comments>http://iregex.org/blog/regex-note-20100621.html#comments</comments>
		<pubDate>Mon, 21 Jun 2010 15:04:15 +0000</pubDate>
		<dc:creator>rex</dc:creator>
				<category><![CDATA[杂项]]></category>
		<category><![CDATA[callback]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[pos]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=128</guid>
		<description><![CDATA[笔记三则，贴在这里。 首字母大小写无关模式 有一段时间，我在写正则表达式来匹配Drug关键字时，经常写出 /viagra&#124;cialis&#124;anti-ed/ 这样的表达式。为了让它更美观，我会给关键词排序；为... ]]></description>
			<content:encoded><![CDATA[<p>笔记三则，贴在这里。</p>
<p><span id="more-128"></span></p>
<h3 style="color: #127ADB; font-size:14px; padding-bottom:3px; padding-top:3px; margin:1.5em 0 1em;">首字母大小写无关模式</h3>
<blockquote style="border-left:2px solid #DDDDDD; margin:15px 30px 0 10px; padding-left:20px;">
<p>有一段时间，我在写正则表达式来匹配<code class="codecolorer text default"><span class="text">Drug</span></code>关键字时，经常写出 <code class="codecolorer perl default"><span class="perl"><span style="color: #339933;">/</span>viagra<span style="color: #339933;">|</span>cialis<span style="color: #339933;">|</span>anti<span style="color: #339933;">-</span>ed<span style="color: #339933;">/</span></span></code> 这样的表达式。为了让它更美观，我会给关键词排序；为了提升速度，我会使用 <code class="codecolorer perl default"><span class="perl"><span style="color: #339933;">/</span><span style="color: #009900;">&#91;</span>Vv<span style="color: #009900;">&#93;</span>iagra<span style="color: #339933;">/</span></span></code> 而非<code class="codecolorer perl default"><span class="perl"><span style="color: #339933;">/</span>viagra<span style="color: #339933;">/</span>i</span></code> ，只让必要的部分进行大小写通配模式。确切地说，我是需要对每个单词的首字母进行大小写无关的匹配。 </p>
<p>我写了这样的一个函数，专门用来批量转换。</p>
<div class="codecolorer-container perl mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#convert regex to sorted list, then provide both lower/upper case for the first letter of each word</span><br />
<span style="color: #666666; font-style: italic;">#luf means lower upper first</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> luf<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># &nbsp; split the regex with the delimiter |</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@arr</span><span style="color: #339933;">=</span><span style="color: #000066;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\|/</span><span style="color: #339933;">,</span><span style="color: #000066;">shift</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># &nbsp; provide both the upper and lower case for the &nbsp;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># &nbsp; first leffer of each word </span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@arr</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">s</span><span style="color: #339933;">/</span><span style="color: #0000ff;">\b</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>a<span style="color: #339933;">-</span>zA<span style="color: #339933;">-</span>Z<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">\l</span><span style="color: #0000ff;">$1</span><span style="color: #0000ff;">\u</span><span style="color: #0000ff;">$1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">/</span>g<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># &nbsp; join the keyword to a regex again</span><br />
&nbsp; &nbsp; <span style="color: #000066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'|'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">@arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000066;">print</span> luf <span style="color: #ff0000;">&quot;sex pill|viagra|cialis|anti-ed&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; the output is:[aA]nti-[eE]d|[cC]ialis|[sS]ex [pP]ill|[vV]iagra</span></div></td></tr></tbody></table></div>
</blockquote>
<h3 style="color: #127ADB; font-size:14px; padding-bottom:3px; padding-top:3px; margin:1.5em 0 1em;">控制全局匹配下次开始的位置</h3>
<blockquote style="border-left:2px solid #DDDDDD; margin:15px 30px 0 10px; padding-left:20px;">
<p>记得jyf曾经问过我，如何控制匹配开始的位置。嗯，现在我可以回答这个问题了。Perl 提供了 pos 函数，可以在 <code class="codecolorer perl default"><span class="perl"><span style="color: #339933;">/</span>g</span></code> 全局匹配中调整下次匹配开始的位置。举例如下：</p>
<div class="codecolorer-container perl mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">$_</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;abcdefg&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/../g</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">print</span> <span style="color: #0000ff;">$&amp;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>其输出结果是每两个字母，即<code class="codecolorer text default"><span class="text">ab, cd, ef</span></code></p>
<p>可以使用 pos($_)来重新定位下一次匹配开始的位置，如：</p>
<div class="codecolorer-container perl mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">$_</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;abcdefg&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/../g</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">pos</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">--;</span> &nbsp;<span style="color: #666666; font-style: italic;">#pos($_)++;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">print</span> <span style="color: #0000ff;">$&amp;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>输出结果：</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">pos($_)--: &nbsp;ab, bc, cd, de, ef, fg.<br />
pos($_)++: &nbsp;ab, de.</div></td></tr></tbody></table></div>
<p>可以阅读 Perl 文档中关于 <a href="http://perldoc.perl.org/functions/pos.html" title="我爱正则表达式" target="_blank">pos</a>的章节获取详细信息。</p>
</blockquote>
<h3 style="color: #127ADB; font-size:14px; padding-bottom:3px; padding-top:3px; margin:1.5em 0 1em;">散列与正则表达式替换</h3>
<blockquote style="border-left:2px solid #DDDDDD; margin:15px 30px 0 10px; padding-left:20px;"><p>
《effective-perl-2e》第三章有这样一个例子（见下面的代码），将特殊符号转义。</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">my %ent = { '&amp;' =&gt; 'amp', '&lt;' =&gt; 'lt', '&gt;' =&gt; 'gt' };<br />
$html =~ s/([&amp;&lt;&gt;])/&amp;$ent{$1};/g;</div></td></tr></tbody></table></div>
<p>这个例子非常非常巧妙。它灵活地运用了散列这种数据结构，将待替换的部分作为 key ，将与其对应的替换内容作为 value 。这样只要有匹配就会捕获，然后将捕获的部分作为 key ，反查到 value 并运用到替换中，体现了高级语言的效率。</p>
<p>不过，这样的 Perl 代码，能否移植到 Python 中呢？ Python 同样支持正则，支持散列（Python 中叫做 Dictionary），但是似乎不支持在替换过程中插入太多花哨的东西（替换行内变量内插）。</p>
<p>查阅 Python 的文档，（在 shell 下 执行 python ，然后 import re，然后 help(re)），：</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sub(pattern, repl, string, count=0)<br />
&nbsp; &nbsp; Return the string obtained by replacing the leftmost<br />
&nbsp; &nbsp; non-overlapping occurrences of the pattern in string by the<br />
&nbsp; &nbsp; replacement repl. &nbsp;repl can be either a string or a callable;<br />
&nbsp; &nbsp; if a string, backslash escapes in it are processed. &nbsp;If it is<br />
&nbsp; &nbsp; a callable, it's passed the match object and must return<br />
&nbsp; &nbsp; a replacement string to be used.</div></td></tr></tbody></table></div>
<p>原来 python 和 php 一样，是支持在替换的过程中使用 callable 回调函数的。该函数的默认参数是一个匹配对象变量。这样一来，问题就简单了：</p>
<div class="codecolorer-container python mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ent=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'&lt;'</span>:<span style="color: #483d8b;">&quot;lt&quot;</span>,<br />
&nbsp; &nbsp; <span style="color: #483d8b;">'&gt;'</span>:<span style="color: #483d8b;">&quot;gt&quot;</span>,<br />
&nbsp; &nbsp; <span style="color: #483d8b;">'&amp;'</span>:<span style="color: #483d8b;">&quot;amp&quot;</span>,<br />
&nbsp; &nbsp; <span style="color: black;">&#125;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> rep<span style="color: black;">&#40;</span>mo<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> ent<span style="color: black;">&#91;</span>mo.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><br />
<br />
html=<span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;([&amp;&lt;&gt;])&quot;</span>,rep, html<span style="color: black;">&#41;</span></div></td></tr></tbody></table></div>
<p>python 替换函数 callback 的关键点在于其参数是一个匹配对象变量。只要明白了这一点，查一下手册，看看该种对象都有哪些属性，一一拿来使用，就能写出灵活高效的 python 正则替换代码。</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/regex-note-20100621.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>搬家完毕</title>
		<link>http://iregex.org/blog/moved.html</link>
		<comments>http://iregex.org/blog/moved.html#comments</comments>
		<pubDate>Tue, 04 May 2010 01:57:32 +0000</pubDate>
		<dc:creator>rex</dc:creator>
				<category><![CDATA[杂项]]></category>
		<category><![CDATA[host]]></category>
		<category><![CDATA[regexbuddy]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=86</guid>
		<description><![CDATA[原空间(homezz.com)到期, 顺便换为法国alwaysdata.com的了. 搬家很顺利, 使用scp将数据从原空间隔空传物过去; 配置好域名dns, 数据文件, 新空间就激活了. 很顺利. 另, 新站的访问速度如何? 可以接受吗?... ]]></description>
			<content:encoded><![CDATA[<p>原空间(homezz.com)到期, 顺便换为法国alwaysdata.com的了. 搬家很顺利, 使用<a href="http://en.wikipedia.org/wiki/Secure_copy">scp</a>将数据从原空间隔空传物过去; 配置好域名dns, 数据文件, 新空间就激活了. 很顺利.</p>
<p>另, 新站的访问速度如何? 可以接受吗?</p>
<p>目前在蕴酿一篇关于&#8221;如何DIY一款像RegexBuddy那样的正则表达式工具&#8221;的博客, 敬请期待.</p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/moved.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>五一搬家</title>
		<link>http://iregex.org/blog/moving.html</link>
		<comments>http://iregex.org/blog/moving.html#comments</comments>
		<pubDate>Thu, 29 Apr 2010 10:53:30 +0000</pubDate>
		<dc:creator>rex</dc:creator>
				<category><![CDATA[杂项]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=85</guid>
		<description><![CDATA[本站在五一期间将更换空间... ]]></description>
			<content:encoded><![CDATA[<p>本站在五一期间将更换空间。</p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/moving.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>效率问题</title>
		<link>http://iregex.org/blog/20091022-efficiency.html</link>
		<comments>http://iregex.org/blog/20091022-efficiency.html#comments</comments>
		<pubDate>Thu, 22 Oct 2009 12:13:46 +0000</pubDate>
		<dc:creator>rex</dc:creator>
				<category><![CDATA[杂项]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=70</guid>
		<description><![CDATA[上周发了篇《两条与密码验证相关的正则表达式问题》。今天看了些python的正则表达式，心血来潮，想看看这几种正则哪种效率较高。代码、运行结果见下。这是为什么呢？ 1234567891011121314151617... ]]></description>
			<content:encoded><![CDATA[<p>上周发了篇《<a href="http://iregex.org/blog/2-regex-problems-about-password-verification.html" target="_blank" title="我爱正则表达式|效率问题">两条与密码验证相关的正则表达式问题</a>》。今天看了些python的正则表达式，心血来潮，想看看这几种正则哪种效率较高。代码、运行结果见下。这是为什么呢？</p>
<div class="codecolorer-container python mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">fpformat</span><br />
Regex1 = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;^(?=[0-9a-zA-Z@%&amp;]*<span style="color: #000099; font-weight: bold;">\d</span>)(?=[0-9a-zA-Z@%&amp;]*[a-zA-Z])(?=[0-9a-zA-Z@%&amp;]*[@%&amp;])[0-9a-zA-Z@%&amp;]{8,}$&quot;</span>, <span style="color: #dc143c;">re</span>.<span style="color: black;">MULTILINE</span><span style="color: black;">&#41;</span><br />
Regex2 = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;^(?=.*<span style="color: #000099; font-weight: bold;">\d</span>)(?=.*[a-zA-Z])(?=.*[@%&amp;])[0-9a-zA-Z@%&amp;]{8,}$&quot;</span>, <span style="color: #dc143c;">re</span>.<span style="color: black;">MULTILINE</span><span style="color: black;">&#41;</span><br />
Regex3 = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;^(?![0-9a-z]{8,16}$|[@%&amp;]{8,16}$)[a-z0-9@%&amp;]{8,16}$&quot;</span>, <span style="color: #dc143c;">re</span>.<span style="color: black;">IGNORECASE</span> |re.<span style="color: black;">MULTILINE</span><span style="color: black;">&#41;</span> <br />
<br />
<br />
<br />
TimesToDo = <span style="color: #ff4500;">1250</span><span style="color: #66cc66;">;</span><br />
TestString = <span style="color: #483d8b;">&quot;&quot;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> i <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;">800</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; TestString += <span style="color: #483d8b;">&quot;aba134babdedfg@&amp;%<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><br />
<br />
StartTime = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>TimesToDo<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp;Regex1.<span style="color: black;">search</span><span style="color: black;">&#40;</span>TestString<span style="color: black;">&#41;</span><br />
Seconds = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - StartTime<br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;R1 takes &quot;</span> + <span style="color: #dc143c;">fpformat</span>.<span style="color: black;">fix</span><span style="color: black;">&#40;</span>Seconds,<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot; seconds&quot;</span><br />
<br />
StartTime = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>TimesToDo<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp;Regex2.<span style="color: black;">search</span><span style="color: black;">&#40;</span>TestString<span style="color: black;">&#41;</span><br />
Seconds = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - StartTime<br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;R2 takes &quot;</span> + <span style="color: #dc143c;">fpformat</span>.<span style="color: black;">fix</span><span style="color: black;">&#40;</span>Seconds,<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot; seconds&quot;</span><br />
<br />
StartTime = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>TimesToDo<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp;Regex3.<span style="color: black;">search</span><span style="color: black;">&#40;</span>TestString<span style="color: black;">&#41;</span><br />
Seconds = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - StartTime<br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;R3 takes &quot;</span> + <span style="color: #dc143c;">fpformat</span>.<span style="color: black;">fix</span><span style="color: black;">&#40;</span>Seconds,<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot; seconds&quot;</span></div></td></tr></tbody></table></div>
<p>运行结果：<br />
<a href="http://iregex.org/blog/20091022-efficiency.html" target="_blank"><img src="http://i293.photobucket.com/albums/mm60/zhasm/iregex/20091022.png" border="0" alt="我爱正则表达式|效率问题"></a> </p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/20091022-efficiency.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>concise perl re table</title>
		<link>http://iregex.org/blog/concise-perl-re-table.html</link>
		<comments>http://iregex.org/blog/concise-perl-re-table.html#comments</comments>
		<pubDate>Wed, 21 Oct 2009 04:19:41 +0000</pubDate>
		<dc:creator>rex</dc:creator>
				<category><![CDATA[杂项]]></category>

		<guid isPermaLink="false">http://iregex.org/blog/concise-perl-re-table.html</guid>
		<description><![CDATA[GDOC链接在这里... ]]></description>
			<content:encoded><![CDATA[<p>GDOC链接在<a href="http://docs.google.com/View?id=dhbb6gcf_8gqm9brgf" target="_blank" title="我爱正则表达式|简明perl正则参考资料">这里</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/concise-perl-re-table.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>正则学习杂感：磨刀不误砍柴功</title>
		<link>http://iregex.org/blog/20090923-learning-regex.html</link>
		<comments>http://iregex.org/blog/20090923-learning-regex.html#comments</comments>
		<pubDate>Wed, 23 Sep 2009 06:05:39 +0000</pubDate>
		<dc:creator>rex</dc:creator>
				<category><![CDATA[杂项]]></category>
		<category><![CDATA[DFA]]></category>
		<category><![CDATA[NFA]]></category>
		<category><![CDATA[正则式]]></category>
		<category><![CDATA[龙书]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=67</guid>
		<description><![CDATA[反复读紫龙书第三章，以及网文Write Your Own Regular Expression Parser（链接在这篇文章中找），连同该网文附带的源码，终于破关。一点感悟，写在这里。 使用正则表达式搜索字串，比逐位进行strcmp... ]]></description>
			<content:encoded><![CDATA[<p>反复读<a href="http://www.douban.com/subject/1134994/" target="_blank">紫龙书</a>第三章，以及网文Write Your Own Regular Expression Parser（链接在<a href="http://iregex.org/blog/20090918-learning-regex-with-some-urls.html" target="_blank">这篇文章</a>中找），连同该网文附带的源码，终于破关。一点感悟，写在这里。</p>
<p>使用正则表达式搜索字串，比逐位进行strcmp这样的方法要复杂N倍，如果说strcmp是只支持＋－×÷的计算器的话，那么正则表达式就是一台现代化的电脑。正则式不用逐位比较，它有自己的一整套方法。</p>
<p>要想在源字串找某个正则式模式，需要先详尽地分析该正则式，其过程是将该正则式解剖成不可再分的字符（称为状态），然后从任意一个状态开始，通过不同的路径（字符输入）可以到达不同的状态。第一个字符之前，叫做初始状态；最后一个字符之后，叫做接收状态。这种是NFA（不确定的有穷自动机），因为一个状态上有可能存在不止一条的转出状态。</p>
<p>一旦做好了这样的状态集合，就可以进行匹配了。每输入一个字符，从开始状态上就会产生不同的状态集合；依次输入完毕，如果所得到状态集合中包含了一个接受状态，就证明所输入的串是匹配的。但是NFA的效率低下。因为从每个状态上转出的路径不止一条，因此就有回溯。为提升效率，可以将NFA转成等效的DFA，即确定的有穷自动机。</p>
<p>DFA的原理是，对于每个状态集合D和给定的输入字符a，最多只有一条通路可走。与NFA的区别是，对于每个单个的状态f和给定的字符a，就有N条路可走。但是DFA也不是生来就万能的，比方说，它有死状态需要消除，同构的状态需要合并，还存在对于复杂正则表达式的DFA的转换效率低、实现复杂度高的情况，等等。虽如此，NFA转换为DFA还是值得的，有道是磨刀不误砍柴功。</p>
<p>对于正则表达式做好了DFA，就可以进行匹配了。每输入一个字符，自动机就转至下一个状态集合，直至输入结尾。如果所达到的状态集合中包含了接受状态，那么匹配就是成功的。</p>
<p>以上就是NFA、DFA的原理。当然，细节是简化过的。否则的话，上面任何一个步骤都有N多的细节，需要使用各种奇怪的数据结构来存储，复杂的成员函数来操作。</p>
<p>说到这里，想起python, pcre，以及其它一些正则表达式库，都有compile一个选项。先编译再执行，一次编译多次执行，文档一再这样强调。这比一上来就execute效率要高，如果重复执行多次的话。以前总是图方便直接就execute了，现在想想，实现一个正则表达式多不容易呀，对于多次重复的匹配，还是老老实实地compile一次才有礼貌。正则引擎在幕后默默无闻地做了那么多工作，不应该老是麻烦它老人家，呵呵。</p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/20090923-learning-regex.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>正则学习杂感，附几枚链接</title>
		<link>http://iregex.org/blog/20090918-learning-regex-with-some-urls.html</link>
		<comments>http://iregex.org/blog/20090918-learning-regex-with-some-urls.html#comments</comments>
		<pubDate>Fri, 18 Sep 2009 02:31:37 +0000</pubDate>
		<dc:creator>rex</dc:creator>
				<category><![CDATA[杂项]]></category>
		<category><![CDATA[DFA]]></category>
		<category><![CDATA[NFA]]></category>
		<category><![CDATA[正则式]]></category>
		<category><![CDATA[龙书]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=65</guid>
		<description><![CDATA[最近在学习编译原理，尤其是正则表达式的实现这一节。从网上搜索到有用的链接，张贴在这里。一个感受是，数学知识是非常有用的，尤其是创造性的编程。如果数学知识足够，完全可以写... ]]></description>
			<content:encoded><![CDATA[<p>最近在学习编译原理，尤其是正则表达式的实现这一节。从网上搜索到有用的链接，张贴在这里。一个感受是，数学知识是非常有用的，尤其是创造性的编程。如果数学知识足够，完全可以写出更加高效的算法；如果数学知识不够，最多只是重复别人已经写好的算法而已。因此在初步学习时，可以不必另造车轮，老老实实地学习已经很成熟的算法，才是正途。</p>
<p>至于正则表达式的实现，其实也只是先有数学思想，然后将其使用编程语言实现而已。准备步骤是先归纳语法树，自顶至下，逐级细分；而实现步骤是从现有的正则表达式自叶至顶，先将正则式转换为NFA图，再优化，转为DFA，这时才可与目标字串进行比较。</p>
<p>在看紫龙书时，感觉到其中从思路到算法，从伪代码到实际代码之间，其转换需要太多的细节来补充。而这些知识，不单来自于对具体语言语法的掌握，还需要合适的数据支持，才能化繁为简，事半功倍。顺便读了一些C++ STL的内容，觉得这些标准库真是相当有用，在构建正则表达式时这些库正好可以物尽其用。STL的作者善莫大焉。</p>
<p>从理论上说，即使没有STL支持的纯C语言，对于实现正则表达式也是没有任何问题的，只不过是自己捎带着再写一遍自己需要的数据结构而已。别说C，就是宏汇编，也可以实现的。如果感兴趣，可以自行到Aogo的www.aogosoft.com上自行搜索他的实现。归结到一句话：思路有了，语言的实现只是翻译。我等初学者是无力想出更高级的实现的，只是在这种翻译过程中，作为一种深化理解和课后练习罢。言归正传，下面是我要推荐的几则链接。</p>
<p>1. 《正则表达式识别》 <a href="http://blog.csdn.net/slavik/archive/2006/09/18/1240386.aspx" target="_blank">http://blog.csdn.net/slavik/archive/2006&#8230;40386.aspx</a> <br />
有图有真相。本文的配图还是挺丰富的。思路也清晰。极具指导意义。<br />
可惜没有源码。该博客的其它相关文章，也值得阅读。</p>
<p>2. 正则表达式 TO NFA表格 <br />
<a href="http://hi.baidu.com/rabbit5455/blog/item/2b2b46f030737bc27931aac8.html" target="_blank">http://hi.baidu.com/rabbit5455/blog/item&#8230;1aac8.html</a><br />
这篇文章主要是提供了源码，源码中附注释。也不错。</p>
<p>3. 《Write Your Own Regular Expression Parser》 <a href="http://www.codeguru.com/cpp/cpp/cpp_mfc/parsing/article.php/c4093" target="_blank">http://www.codeguru.com/cpp/cpp/cpp_mfc/&#8230;.php/c4093</a></p>
<p>本文是英文文章，有图示，有源码，很不错。我正在边阅读，边学习，边翻译，一边自行实现。建议直接阅读英文资料，不必等我的翻译。本文的代码只提供了*,|，以及连接操作的实现。不过也对其它操作的实现给出了指导。我已经在源代码基础上添加了问号和加号的支持。</p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/20090918-learning-regex-with-some-urls.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DIY万能通配符</title>
		<link>http://iregex.org/blog/diy-match-all-mode-dot.html</link>
		<comments>http://iregex.org/blog/diy-match-all-mode-dot.html#comments</comments>
		<pubDate>Tue, 11 Aug 2009 08:22:08 +0000</pubDate>
		<dc:creator>rex</dc:creator>
				<category><![CDATA[杂项]]></category>
		<category><![CDATA[dot]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=63</guid>
		<description><![CDATA[指定了点号匹配换行符模式，就可以使用点号来匹配包括换行符在内的任意字符了。不过，JavaScript的正则中无法设置点号通配模式，但是我们可以创造一个可以匹配换行符的点号，如下： 1&#40;... ]]></description>
			<content:encoded><![CDATA[<p>指定了点号匹配换行符模式，就可以使用点号来匹配包括换行符在内的任意字符了。不过，JavaScript的正则中无法设置点号通配模式，但是我们可以创造一个可以匹配换行符的点号，如下：</p>
<div class="codecolorer-container perl mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#40;</span><span style="color: #339933;">?:.|</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">\r</span><span style="color: #0000ff;">\n</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>除此之外，还可以使用互逆的元字符，来构造这样的万能通配符，例如</p>
<div class="codecolorer-container perl mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#91;</span>\<span style="color: #000066;">s</span><span style="color: #0000ff;">\S</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">\d</span><span style="color: #0000ff;">\D</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">\w</span><span style="color: #0000ff;">\W</span><span style="color: #009900;">&#93;</span></div></td></tr></tbody></table></div>
<p>使用普通字符也可以的。因为我们可以将全部字符划分为a和非a，</p>
<div class="codecolorer-container perl mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#40;</span><span style="color: #339933;">?:</span>a<span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?!</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/diy-match-all-mode-dot.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>正则杂谈：从&#8220;评论已关闭&#8221;到正则表达式之眼</title>
		<link>http://iregex.org/blog/from-comment-closed-to-regex-crafting.html</link>
		<comments>http://iregex.org/blog/from-comment-closed-to-regex-crafting.html#comments</comments>
		<pubDate>Wed, 27 May 2009 02:07:50 +0000</pubDate>
		<dc:creator>rex</dc:creator>
				<category><![CDATA[杂项]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://iregex.org/blog/from-comment-closed-to-regex-crafting.html</guid>
		<description><![CDATA[近来沸沸扬扬地一条饭否消息是评论已关闭，这个关键词真的选的很好，它一下子把所有的一类敏感问题都串起来了，找到了该类问题的共同特征。至于为什么关闭评论，关闭的标准是什么，... ]]></description>
			<content:encoded><![CDATA[<p>近来沸沸扬扬地一条饭否消息是<a href="http://fanfou.com/home#search?q=%E8%AF%84%E8%AE%BA%E5%B7%B2%E5%85%B3%E9%97%AD" rel="nofollow" target="_blank">评论已关闭</a>，这个关键词真的选的很好，它一下子把所有的一类敏感问题都串起来了，找到了该类问题的共同特征。至于为什么关闭评论，关闭的标准是什么，这里都不做探讨，只是从正则角度谈论一下技术而已。</p>
<p>所谓正则表达式，其实是一种抽象，是从现有的文本中找到规律，然后用正则的语言描述出来。例如，上次有人问，以http开头的，以空格结尾的正则式如何写？这个问题不难，我们只要符合这两个要求即可，即^(http\S+)\s。至于http后面的冒号、双斜杠，都不必劳神去匹配。这样写出来的正则表达式简洁明快，清晰易读。当然，从效率出发，越精确的正则式越有利于尽早失败，有利于提升效率。权衡写的效率与执行的效率，其过程也是很不错的。</p>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/from-comment-closed-to-regex-crafting.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>数字转美元程序</title>
		<link>http://iregex.org/blog/convert-digits-to-english-value.html</link>
		<comments>http://iregex.org/blog/convert-digits-to-english-value.html#comments</comments>
		<pubDate>Sun, 15 Feb 2009 05:09:08 +0000</pubDate>
		<dc:creator>rex</dc:creator>
				<category><![CDATA[杂项]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[美元]]></category>

		<guid isPermaLink="false">http://iregex.org/?p=57</guid>
		<description><![CDATA[本程序将数字转换为英文的美元数，如： 输入 1./num2eng.pl 1,100,834.10 则输出： 1Total: Say US Dollars One Million One Hundred Thundsand Eight Hundred and Thirty-Four and Ten Cents Only. 注意事项： 整数部分可以使用半... ]]></description>
			<content:encoded><![CDATA[<p>本程序将数字转换为英文的美元数，如： 输入</p>
<div class="codecolorer-container bash mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">.<span style="color: #000000; font-weight: bold;">/</span>num2eng.pl <span style="color: #000000;">1</span>,<span style="color: #000000;">100</span>,<span style="color: #000000;">834.10</span></div></td></tr></tbody></table></div>
<p>则输出：</p>
<div class="codecolorer-container bash mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Total: Say US Dollars One Million One Hundred Thundsand Eight Hundred and Thirty-Four and Ten Cents Only.</div></td></tr></tbody></table></div>
<p>注意事项：</p>
<ol>
<li>整数部分可以使用半角的逗号、空格、单引号、下划线、中划线分隔。</li>
<li>分隔符的位置可以任意（每3位可，每4位也可），可以任意组合（可以混合使用上述的分隔符）。</li>
<li>如果使用单引号，请注意在最外边加上双引号以免转义。
</li>
</ol>
<p>完整程序：<br />
<span id="more-57"></span></p>
<div class="codecolorer-container perl mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br />175<br />176<br />177<br />178<br />179<br />180<br />181<br />182<br />183<br />184<br />185<br />186<br />187<br />188<br />189<br />190<br />191<br />192<br />193<br />194<br />195<br />196<br />197<br />198<br />199<br />200<br />201<br />202<br />203<br />204<br />205<br />206<br />207<br />208<br />209<br />210<br />211<br />212<br />213<br />214<br />215<br />216<br />217<br />218<br />219<br /></div></td><td><div class="perl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span><br />
<span style="color: #666666; font-style: italic;">#</span><br />
<span style="color: #666666; font-style: italic;">#rex[at]zhasm[dot]com</span><br />
<span style="color: #666666; font-style: italic;">#13 Feb 2009 on perl 5.10 and Ubuntu 8.10</span><br />
<br />
<span style="color: #0000ff;">@single</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;One&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Two&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Three&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Four&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Five&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Six&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Seven&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Eight&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Nine&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Ten&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Eleven&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Twelve&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Thirteen&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Fourteen&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Fifteen&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Sixteen&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Seventeen&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Eighteen&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Nineteen&quot;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #0000ff;">@tens</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Ten&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Twenty&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Thirty&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Forty&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Fifty&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Sixty&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Seventy&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Eighty&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Ninety&quot;</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #0000ff;">@scale</span><span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Thundsand&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Million&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Billion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Trillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Quadrillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Quintillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Sextillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Septillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Octillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Nonillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Decillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Undecillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Duodecillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Tredecillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Quattuordecillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Quindecillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Sexdecillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Septendecillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Octodecillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Novemdecillion&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;Vigintillion&quot;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> dot<br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#$dot=1*$dot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">return</span> <span style="color: #0000ff;">$single</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$dot</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$ten</span><span style="color: #339933;">=</span><span style="color: #000066;">int</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$extra</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$dot</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$ten</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">return</span> <span style="color: #ff0000;">&quot;$tens[$ten] $single[$extra]&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">length</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">return</span> <span style="color: #ff0000;">&quot;$tens[$dot]&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> debug<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">print</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span> <br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">sub</span> hundred<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$result</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$number</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">999</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; debug<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;too large&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">return</span> <span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$c</span><span style="color: #339933;">=</span><span style="color: #000066;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$number</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$b</span><span style="color: #339933;">=</span><span style="color: #000066;">int</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$number</span><span style="color: #339933;">-</span><span style="color: #0000ff;">$c</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$a</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$number</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$cc</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$bb</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$aa</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$c</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$cc</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;$single[$c] Hundred&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$a</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$b</span><span style="color: #339933;">&gt;=</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$bb</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;$tens[$b]-$single[$a]&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$b</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$bb</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$single</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$b</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$a</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #0000ff;">$a</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$b</span><span style="color: #339933;">&gt;=</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$bb</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;$tens[$b]&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$b</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#includeing the condition when $b==0;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$bb</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$single</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$b</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$c</span> <span style="color: #b1b100;">and</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$a</span> <span style="color: #b1b100;">or</span> <span style="color: #0000ff;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">return</span> <span style="color: #ff0000;">&quot;$cc and $bb&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">elsif</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$c</span> <span style="color: #b1b100;">and</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$a</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span> <span style="color: #b1b100;">and</span> <span style="color: #0000ff;">$b</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">return</span> <span style="color: #0000ff;">$cc</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">elsif</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #0000ff;">$c</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">return</span> &nbsp;<span style="color: #ff0000;">&quot;and $bb&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> integer<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$money</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$index</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$string</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$flag</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$flag</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$small</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$money</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">1000</span> <span style="color: #b1b100;">and</span> <span style="color: #0000ff;">$money</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$small</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$money</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$flag</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$money</span><span style="color: #339933;">&gt;=</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$small</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$money</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$money</span><span style="color: #339933;">=</span><span style="color: #000066;">int</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$money</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$small</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$string</span><span style="color: #339933;">=</span>hundred<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$small</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot; $scale[$index] &quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$string</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$index</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$string</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/\s+$//</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$string</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/^\s+//</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$string</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/^and\s+//i</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$string</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">sub</span> convert<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$digits</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dot</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0.0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$integer</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$result</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$digits</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/[-,_' ]//g</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$digits</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(\d*)\.(\d*)/</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$integer</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$dot</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$2</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$digits</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(\d+)/</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$integer</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$integer</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$result</span> <span style="color: #339933;">.=</span> integer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$integer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span> <span style="color: #b1b100;">and</span> <span style="color: #0000ff;">$integer</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$result</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">&quot; and &quot;</span><span style="color: #339933;">.</span> dot<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot; Cent&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">$result</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">&quot; and &quot;</span><span style="color: #339933;">.</span> dot<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot; Cents&quot;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span> <span style="color: #b1b100;">and</span> <span style="color: #0000ff;">$integer</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$result</span> <span style="color: #339933;">.=</span> dot<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot; Cent&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">$result</span> <span style="color: #339933;">.=</span> dot<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$dot</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot; Cents&quot;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$result</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;Total: Say US Dollars &quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">$result</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot; Only.&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$money</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #339933;">;</span> <br />
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$line</span><span style="color: #339933;">=</span>convert<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$money</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://iregex.org/blog/convert-digits-to-english-value.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
