title: How does this regular expression work?
body: <p>From <a href="http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/" rel="noreferrer">this article</a>,</p>
  
  <p><code>/^1?$|^(11+?)\1+$/</code> checks whether a number(its value in unary) is prime or not.</p>
  
  <p>Using this, <code>perl -l -e '(1 x $_) !~ /^1?$|^(11+?)\1+$/ &amp;&amp; print while ++$_;'</code> returns a list of prime numbers.</p>
  
  <p>I do not have enough experience with Perl, but what I understand is that the regular expression will be <strong>true</strong> for a number that is not prime. So, if we print all numbers that do not produce a <strong>true</strong> with this expression, we have a list of prime numbers. Thats what the perl query is trying to do.</p>
  
  <p>About the regex part,</p>
  
  <p><code>^1?$</code> part is for counting 1 as <strong>not prime</strong></p>
  
  <p><code>^(11+?)\1+$</code> is for matching not prime numbers starting from 4.</p>
  
  <hr>
  
  <p>What I do not understand is why is the <code>?</code> in the regex needed at all.
  According to me <code>/^1$|^(11+)\1+$/</code> should be just fine and actually</p>
  
  <p><code>perl -l -e '(1 x $_) !~ /^1$|^(11+)\1+$/ &amp;&amp; print while ++$_;'</code>
  gives me the same set of prime numbers.</p>
  
  <p><strong>Is there any flaw in my understanding of the regular expression? Why are the <code>?</code>s needed?</strong></p>
  
  <p>Isn't <code>?</code> supposed to match zero or one occurrence of the expression preceding it?</p>