<?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>Barklund.org &#187; Regular Expressions</title>
	<atom:link href="http://www.barklund.org/blog/category/programming/regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.barklund.org/blog</link>
	<description>work smarter when building current web trends</description>
	<lastBuildDate>Wed, 26 May 2010 09:49:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>iPad detection the right way</title>
		<link>http://www.barklund.org/blog/2010/04/23/ipad-detection/</link>
		<comments>http://www.barklund.org/blog/2010/04/23/ipad-detection/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 09:22:34 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Konstellation]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[Trends]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=760</guid>
		<description><![CDATA[Here at Konstellation, we just launched the PageGangster for mobile devices including iPad, iPhone, iPod touch and Android. I developed the clientside for this in JavaScript and we launched it with automatic user agent detection serverside. But this had some problems. I read some info about this and conclude, that some iPad user agent strings [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/11/23/iphone-developer-boycott-in-the-works/' rel='bookmark' title='Permanent Link: The iPhone developer boycott in the works'>The iPhone developer boycott in the works</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/11/coordinate-proxy-january-2010-idea/' rel='bookmark' title='Permanent Link: Coordinate-Proxy &#8211; January 2010 Ideas'>Coordinate-Proxy &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/07/app-idea-store-january-2010-idea/' rel='bookmark' title='Permanent Link: App Idea Store &#8211; January 2010 Ideas'>App Idea Store &#8211; January 2010 Ideas</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div id="attachment_766" class="wp-caption alignright" style="width: 160px"><img src="http://www.barklund.org/blog/wp-content/uploads/2010/04/ipad-300x241.jpg" alt="iPad in action" title="iPad" width="150" height="120" class="size-medium wp-image-766" /><p class="wp-caption-text">The famous large flat thing slightly more useful than a rock</p></div>
<p>Here at <a href="http://konstellation.dk/" title="Konstellation homepage">Konstellation</a>, we just launched the <a href="http://pagegangster.com" title="PageGangster - convert your PDF to an experience">PageGangster</a> for <a href="http://www.pagegangster.com/mobile-devices/" title="Introduction about PageGangster for mobile devices">mobile devices</a> including iPad, iPhone, iPod touch and Android. I developed the clientside for this in JavaScript and we launched it with automatic user agent detection serverside. But this had some problems.</p>
<p><span id="more-760"></span></p>
<p>I read <a href="http://www.nczonline.net/blog/2010/04/06/ipad-web-development-tips/" title="iPad web development tips from nczonline.net">some info</a> about this and conclude, that some iPad user agent strings include the word &#8220;iPhone&#8221; and some users of all types of devices fake their user agent strings.</p>
<p>But then I read on the above link, that no matter what, the <code>navigator.platform</code> attribute would always be set to the right type of device. So in the regular web version of the publication viewer we have now (which uses Flash), we have added a JavaScript detecting if the serverside script did not redirect the client properly:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>navigator <span style="color: #339933;">&amp;&amp;</span> navigator.<span style="color: #660066;">platform</span> <span style="color: #339933;">&amp;&amp;</span> navigator.<span style="color: #660066;">platform</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^(iPad|iPod|iPhone)$/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	location.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">href</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;?force-mobile&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It seems simple, but solves the problem of some users seeing the dreaded blue box instead of their publication optimized for their device.</p>
<p>On another node, this experience of developing a web application for the iPad/iPhone was very rewarding and I gained many insights, which might spawn other posts. And if you have any questions about it in any way, feel free to ask here &#8211; but please do not submit bug reports about the PageGangster for mobile &#8211; do that via <a href="mailto:info@pagegangster.com">info@pagegangster.com</a> instead.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/11/23/iphone-developer-boycott-in-the-works/' rel='bookmark' title='Permanent Link: The iPhone developer boycott in the works'>The iPhone developer boycott in the works</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/11/coordinate-proxy-january-2010-idea/' rel='bookmark' title='Permanent Link: Coordinate-Proxy &#8211; January 2010 Ideas'>Coordinate-Proxy &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/07/app-idea-store-january-2010-idea/' rel='bookmark' title='Permanent Link: App Idea Store &#8211; January 2010 Ideas'>App Idea Store &#8211; January 2010 Ideas</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2010/04/23/ipad-detection/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using the regular expression backtracking engine for prime number magic</title>
		<link>http://www.barklund.org/blog/2009/07/16/regular-expresssion-backtracking-prime-magic/</link>
		<comments>http://www.barklund.org/blog/2009/07/16/regular-expresssion-backtracking-prime-magic/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 14:13:28 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[Trends]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=212</guid>
		<description><![CDATA[I just saw the most impressive, intelligent use of the regular expression backtracking engine and simply have to re-post it in order to give my own decryption of the infinitely simple expression. In short, it is a regular expression, that only matches non-prime sequences of 1&#8242;s. And it goes like this: /^1?$&#124;^(11+?)\1+$/ I will try [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>I just <a href="http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/" title="Avinash Meetoo: Blog » A regular expression to check for prime numbers">saw the most impressive, intelligent use of the regular expression backtracking engine</a> and simply have to re-post it in order to give my own decryption of the infinitely simple expression. In short, it is a regular expression, that only matches non-prime sequences of 1&#8242;s. And it goes like this:</p>

<div class="wp_syntax"><div class="code"><pre class="regexp" style="font-family:monospace;">/^1?$|^(11+?)\1+$/</pre></div></div>

<p>I will try to explain it in a simple way and then do some benchmarking on this simple expression, a slightly optimized version of the expression as well as compare it to more optimal algorithms.</p>
<p><span id="more-212"></span></p>
<p>I can explain it very simply. The first part of the expression check the special cases of zero or one 1&#8242;s. The second part checks whether the number of 1&#8242;s is a multiple of another substring of 1&#8242;s. It does that by un-greedily matching the shortest possible string of 1&#8242;s larger than one (the shortest possible is initially of course two 1&#8242;s) as the first group and then checks whether the remaining number of 1&#8242;s is evenly divisible by this by a factor larger that zero &#8211; that is, if there were six 1&#8242;s, the remaining number of 1&#8242;s after having matched the first two would be four, which is evenly divisible by two by a factor larger than zero. If we tried to match nine 1&#8242;s, the first attempt at matching two 1&#8242;s in the first group would fail, as the remaining seven 1&#8242;s is not evenly divisible by this. But the regular expression backtracking engine would then backtrack to matching three 1&#8242;s in the first group, and then the remaining six 1&#8242;s would be evenly divisible by this number, and the pattern matches &#8211; thus 9 is <strong>not</strong> prime as expected.</p>
<p>The penalty? Well, try to match eleven 1&#8242;s, the first group would initially be set to two 1&#8242;s, and the remaining expression fails, the initial group would then be set to three 1&#8242;s, and the remaining expression would still fail, then the initial expression would be set to four 1&#8242;s (which is just stupid prime-checking-wise &#8211; 2 was not a factor, then how could 4 be?) and it would still fail &#8211; and so on until the engine would match all eleven 1&#8242;s as the first group and check if the remaining zero 1&#8242;s is evenly divisible (by a factor larger than zero of course) which it isn&#8217;t and only then would the expression fail. It is thus highly expensive, as there is of course no need to check if any number higher than the square root of the number to check divides the number evenly, if all numbers below the square root doesn&#8217;t.</p>
<p>To do some speed-testing, let&#8217;s try to check the number 9017 (which is the product of the somewhat large primes 71 and 127 and thus not prime) and the number 9029 (which is next prime after 9017). And for the fun of it, let&#8217;s test it in JavaScript right here in the browser:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> check1<span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t1 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>number<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> str <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t2 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^1?$|^(11+?)\1+$/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t3 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Time of generation: &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>t2<span style="color: #339933;">-</span>t1<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; ms, time of checking: &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>t3<span style="color: #339933;">-</span>t2<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; ms, result: &quot;</span><span style="color: #339933;">+</span>number<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; is &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>result <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;not &quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;prime&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
check1<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">9017</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;Time of generation: 2 ms, time of checking: 6 ms, result: 9017 is not prime&quot;</span>
check1<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">9029</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;Time of generation: 2 ms, time of checking: 64 ms, result: 9029 is prime&quot;</span></pre></div></div>

<p>64 ms for failing to validate 9029 as not prime is way too long, so let&#8217;s simply change &#8220;(11+?)&#8221; to &#8220;(1{2,x}?)&#8221; with x being the square root of the given number truncated to an integer:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> check2<span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t1 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>number<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> str <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> sqrt <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> re <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;^1?$|^(1{2,&quot;</span><span style="color: #339933;">+</span>sqrt<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;}?)<span style="color: #000099; font-weight: bold;">\\</span>1+$&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t2 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> re.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t3 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Time of generation: &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>t2<span style="color: #339933;">-</span>t1<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; ms, time of checking: &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>t3<span style="color: #339933;">-</span>t2<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; ms, result: &quot;</span><span style="color: #339933;">+</span>number<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; is &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>result <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;not &quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;prime&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
check2<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">9017</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;Time of generation: 2 ms, time of checking: 11 ms, result: 9017 is not prime&quot;</span>
check2<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">9029</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;Time of generation: 2 ms, time of checking: 12 ms, result: 9029 is prime&quot;</span></pre></div></div>

<p>This actually increased the time to check for non-primes (as &#8220;{a,b}?&#8221; is more expensive than simply &#8220;+?&#8221; as the recursion needs to keep track of the limits) but greatly reduced the time it took to check for primes, as it only checks up until 95 1&#8242;s (95 = floor(sqrt(9029))) and not all the way up to 9029 1&#8242;s.</p>
<p>But just for the fun of it, let&#8217;s compare with more common check&#8217;s &#8211; like <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" title="Sieve of Eratosthenes on the English Wikipedia">Sieve of Eratosthenes</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> check3<span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t1 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> nonprimes <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> sqrt <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> pointer <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>pointer <span style="color: #339933;">&lt;=</span> sqrt <span style="color: #339933;">&amp;&amp;</span> nonprimes<span style="color: #009900;">&#91;</span>number<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> pointer<span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> number<span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> pointer<span style="color: #009900;">&#41;</span> nonprimes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>pointer <span style="color: #339933;">&lt;=</span> sqrt <span style="color: #339933;">&amp;&amp;</span> nonprimes<span style="color: #009900;">&#91;</span>pointer<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> nonprimes<span style="color: #009900;">&#91;</span>number<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t2 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Time of checking: &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>t2<span style="color: #339933;">-</span>t1<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; ms, result: &quot;</span><span style="color: #339933;">+</span>number<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; is &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>result <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;not &quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;prime&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
check3<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">9017</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;Time of checking: 1 ms, result: 9017 is not prime&quot;</span>
check3<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">9029</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;Time of checking: 1 ms, result: 9029 is prime&quot;</span></pre></div></div>

<p>It is pretty clear, that the regular expression does not in any way compare to an optimal algorithm &#8211; but the original regular expression referenced in the top is none the less a very intelligent hack on the regular expression engine mechanics.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/07/16/regular-expresssion-backtracking-prime-magic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
