<?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>Wojciech Jamrozy blog &#187; perl 5.10</title>
	<atom:link href="http://www.wojtekrj.net/tag/perl-5-10/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wojtekrj.net</link>
	<description>Blog about computer science, programming and linux</description>
	<lastBuildDate>Sun, 23 Oct 2011 21:57:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>[Perl] How to check divisibility by 3 using regular expressions</title>
		<link>http://www.wojtekrj.net/2009/08/perl-how-to-check-divisibility-by-3-using-regular-expressions/</link>
		<comments>http://www.wojtekrj.net/2009/08/perl-how-to-check-divisibility-by-3-using-regular-expressions/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 14:47:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[perl 5.10]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.wojtekrj.net/?p=259</guid>
		<description><![CDATA[Here is a Perl regular expression using Perl 5.10 extensions (this is a finite state automaton): ^&#40;?&#38;s&#41;&#40;?&#40;DEFINE&#41; &#40;?&#60;s&#62;&#40;&#91;0369&#93;&#40;?&#38;z&#41;&#124;&#91;147&#93;&#40;?&#38;j&#41;&#124;&#91;258&#93;&#40;?&#38;d&#41;&#41;&#41; &#40;?&#60;z&#62;&#40;$&#124;&#91;0369&#93;&#40;?&#38;z&#41;&#124;&#91;147&#93;&#40;?&#38;j&#41;&#124;&#91;258&#93;&#40;?&#38;d&#41;&#41;&#41; &#40;?&#60;j&#62;&#40;&#91;0369&#93;&#40;?&#38;j&#41;&#124;&#91;147&#93;&#40;?&#38;d&#41;&#124;&#91;258&#93;&#40;?&#38;z&#41;&#41;&#41; &#40;?&#60;d&#62;&#40;&#91;0369&#93;&#40;?&#38;d&#41;&#124;&#91;147&#93;&#40;?&#38;z&#41;&#124;&#91;258&#93;&#40;?&#38;j&#41;&#41;&#41;&#41; States: s &#8211; start state z &#8211; number before modulo 3 is 0 j &#8211; number before modulo 3 is 1 d &#8211; numer before modulo 3 is 2 There is a distinction between [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a Perl regular expression using Perl 5.10 extensions (this is a finite state automaton):</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #339933;">^</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;s</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #009900;">&#40;</span>DEFINE<span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #009999;">&lt;s&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>0369<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;z</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">147</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;j</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">258</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;d</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #009999;">&lt;z&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$|</span><span style="color: #009900;">&#91;</span>0369<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;z</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">147</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;j</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">258</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;d</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #009999;">&lt;j&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>0369<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;j</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">147</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;d</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">258</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;z</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #009999;">&lt;d&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>0369<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;d</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">147</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;z</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">258</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">?</span><span style="color: #0000ff;">&amp;j</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><span id="more-259"></span><br />
States:<br />
s &#8211; start state<br />
z &#8211; number before modulo 3 is 0<br />
j &#8211; number before modulo 3 is 1<br />
d &#8211; numer before modulo 3 is 2<br />
There is a distinction between s and z states in order to prevent expression to accept empty string as numer divisible by 3.</p>
<p>Here is &#8220;true&#8221; regular expression (without Perl 5.10 extensions):</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #339933;">^</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>0369<span style="color: #009900;">&#93;</span><span style="color: #339933;">+|</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">258</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>0369<span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">258</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>0369<span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">147</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>0369<span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">147</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">258</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>0369<span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">258</span><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: #cc66cc;">147</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>0369<span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">147</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>0369<span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">258</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>0369<span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">258</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">|</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">147</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>0369<span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">147</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>$</pre></div></div>

<p>Usage:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">;&lt;&gt;;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^([0369]+|([258][0369]*([258][0369]*[147][0369]*)*([147]|[258][0369]*[258])|[147][0369]*([147][0369]*[258][0369]*)*([258]|[147][0369]*[147])))+$/</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000066;">print</span> <span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The script will print all lines of standard input which are numbers divisible by 3.<br />
Here is an useful link: <a href="http://perldoc.perl.org/perlretut.html" target="_blank">http://perldoc.perl.org/perlretut.html</a></p>
<p><a class="a2a_button_wykop" href="http://www.addtoany.com/add_to/wykop?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fperl-how-to-check-divisibility-by-3-using-regular-expressions%2F&amp;linkname=%5BPerl%5D%20How%20to%20check%20divisibility%20by%203%20using%20regular%20expressions" title="Wykop" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/wykop.png" width="16" height="16" alt="Wykop"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fperl-how-to-check-divisibility-by-3-using-regular-expressions%2F&amp;linkname=%5BPerl%5D%20How%20to%20check%20divisibility%20by%203%20using%20regular%20expressions" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fperl-how-to-check-divisibility-by-3-using-regular-expressions%2F&amp;linkname=%5BPerl%5D%20How%20to%20check%20divisibility%20by%203%20using%20regular%20expressions" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_technorati_favorites" href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fperl-how-to-check-divisibility-by-3-using-regular-expressions%2F&amp;linkname=%5BPerl%5D%20How%20to%20check%20divisibility%20by%203%20using%20regular%20expressions" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a><a class="a2a_button_digg" href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fperl-how-to-check-divisibility-by-3-using-regular-expressions%2F&amp;linkname=%5BPerl%5D%20How%20to%20check%20divisibility%20by%203%20using%20regular%20expressions" title="Digg" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fperl-how-to-check-divisibility-by-3-using-regular-expressions%2F&amp;linkname=%5BPerl%5D%20How%20to%20check%20divisibility%20by%203%20using%20regular%20expressions" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_msdn" href="http://www.addtoany.com/add_to/msdn?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fperl-how-to-check-divisibility-by-3-using-regular-expressions%2F&amp;linkname=%5BPerl%5D%20How%20to%20check%20divisibility%20by%203%20using%20regular%20expressions" title="MSDN" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/msdn.png" width="16" height="16" alt="MSDN"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fperl-how-to-check-divisibility-by-3-using-regular-expressions%2F&amp;linkname=%5BPerl%5D%20How%20to%20check%20divisibility%20by%203%20using%20regular%20expressions" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_technotizie" href="http://www.addtoany.com/add_to/technotizie?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fperl-how-to-check-divisibility-by-3-using-regular-expressions%2F&amp;linkname=%5BPerl%5D%20How%20to%20check%20divisibility%20by%203%20using%20regular%20expressions" title="Technotizie" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technotizie.png" width="16" height="16" alt="Technotizie"/></a><a class="a2a_button_technet" href="http://www.addtoany.com/add_to/technet?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fperl-how-to-check-divisibility-by-3-using-regular-expressions%2F&amp;linkname=%5BPerl%5D%20How%20to%20check%20divisibility%20by%203%20using%20regular%20expressions" title="TechNet" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technet.png" width="16" height="16" alt="TechNet"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fperl-how-to-check-divisibility-by-3-using-regular-expressions%2F&amp;title=%5BPerl%5D%20How%20to%20check%20divisibility%20by%203%20using%20regular%20expressions" id="wpa2a_2">Share/Save</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.wojtekrj.net/2009/08/perl-how-to-check-divisibility-by-3-using-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

